Yii2快速总结

1.项目目录php

后面的总结都是基于这个项目目录,注意:这个Yii2框架的basic版本原始的目录结构,未做任何更改。web

2.新建控制器以及视图chrome

controllers这个目录下的SiteController.php这是一个site控制器,咱们在里面添加一个action:数据库

public function actionSay($message = 'Hello'){
        return $this->render('say',['message'=>$message]);
    } 

这个action名为say,默认参数$message的值为'Hello',而后咱们再去views\site目录下新建一个视图页面say.php:bootstrap

<?php

/* @var $this yii\web\View */

use yii\helpers\Html;

?>
<div>
    <h1><?= Html::encode($message) ?></h1>

    <p>
        This is the Say page. You may modify the following file to customize its content:
    </p>

    <code><?= __FILE__ ?></code>
</div>

最后浏览器访问:http://localhost/basic/web/index.php?r=site/say&message=Hello+world;浏览器

经过site控制器访问名为say的action,传进来参数,而后这个action跳转(render)到say页面,而且用相似于el表达式接收action传来的值。安全

3.Models之ActiveRecordapp

models/Country.php(表明和读取country数据表的数据,这个类里面不须要写什么)框架

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Country extends ActiveRecord
{
}

controllers/CountryController(就和SiteController同样是个控制器,里面能够写actionXXX)iview

<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country; class CountryController extends Controller
{
    public function actionIndex()
    {
        $query = Country::find();
        $pagination = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => $query->count(),
        ]);
        $countries = $query->orderBy('name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render('index', [
            'countries' => $countries,
            'pagination' => $pagination,
        ]);
    }
}

这个控制器的actionIndex调用models/Country类来进行Country表的全字段查询,而后转到views/country/index.php页面

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($countries as $country): ?>
    <li>
        <?= Html::encode("{$country->name} ({$country->code})") ?>:
        <?= $country->population ?>
    </li>
<?php endforeach; ?>
</ul>

<?= LinkPager::widget(['pagination' => $pagination]) ?>

最终页面:

4.Gii生成代码

一般在config/web.php最后面,已经为该项目开启了Gii模块,部分代码以下

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}

若是你经过本机之外的机器访问 Gii,请求会被出于安全缘由拒绝。 你能够配置 Gii 为其添加容许访问的 IP 地址(allowdIPs)

而后浏览器输入类如http://localhost/basic/web/index.php?r=gii,就能够看到Gii的界面了,你能够用chrome的翻译工具将该页面翻译为中文 (T_T)。

now,开始学着去用它吧~~~

模型生成器:

这就和hibernate由数据表生成持久化类一个道理,操做简便,输入表名和对应生成的类名,而后Priview-->Generator就能在你的项目根目录下的models目录下生成类了。这里拿country数据表做为例子,生成的类是models/Country.php。

CRUD生成器:

这就是一个数据库的增删改查,点击,而后填写表单,

  • Model Class: app\models\Country
  • Search Model Class: app\models\CountrySearch
  • Controller Class: app\controllers\CountryController
  • View Path: @app/views/country

而后Priview-->Generator就能在你的项目里生成n多的文件了。

而后浏览器输入:http://localhost/basic/web/index.php?r=country/index

5.

相关文章
相关标签/搜索