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.

相關文章
相關標籤/搜索