• Yii2快速总结


    1.项目目录

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

    2.新建控制器以及视图

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

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

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

    <?php
    
    /* @var $this yiiwebView */
    
    use yiihelpersHtml;
    
    ?>
    <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 appmodels;
    
    use yiidbActiveRecord;
    
    class Country extends ActiveRecord
    {
    }

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

    <?php
    
    namespace appcontrollers;
    
    use yiiwebController;
    use yiidataPagination;
    use appmodelsCountry;
    
    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 yiihelpersHtml;
    use yiiwidgetsLinkPager;
    ?>
    <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' => 'yiidebugModule',
            // 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' => 'yiigiiModule',
            // 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: appmodelsCountry
    • Search Model Class: appmodelsCountrySearch
    • Controller Class: appcontrollersCountryController
    • View Path: @app/views/country

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

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

    5.

  • 相关阅读:
    close()和shutdown()函数
    select、poll、epoll之间的区别总结[整理]
    多路复用I/O模型epoll() 模型 代码实现
    多路复用I/O模型poll() 模型 代码实现
    多路复用I/O模型select() 模型 代码实现
    socket编程之select(),poll(),epoll()
    Android Studio 导入项目 出现安装Error:Cause: failed to find target with hash string 'android-23' 等错误
    Oracle大总结
    MySQL java连接被拒绝:java.sql.SQLException: Access denied for user 'root'@'****' (using password: YES)
    解决mysql中只能通过localhost访问不能通过ip访问的问题
  • 原文地址:https://www.cnblogs.com/eco-just/p/8717725.html
Copyright © 2020-2023  润新知