Yii2快速总结

本文介绍了Yii2框架的基本使用方法,包括项目目录结构、控制器及视图创建、ActiveRecord模型应用、Gii代码生成器使用等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.项目目录

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

2.新建控制器以及视图

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

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

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

<?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之ActiveRecord

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

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Country extends ActiveRecord
{
}

controllers/CountryController(就和SiteController一样是个控制器,里面可以写actionXXX)

<?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.

 

转载于:https://www.cnblogs.com/eco-just/p/8717725.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值