yii2框架学习笔记

1.去掉yii2模版默认的头部和脚部的两种方法:

  (1) 第一种

1 $this->layout = false;
2 $this->render('index');

  (2) 第二种(partial意为局部的)

$this->renderPartial('index');

2.用AppAsset加载静态资源:

在对应前后台目录下的assets目录下的AppAsset.php文件中加入对应的css、js文件路径,然后再html页面进行注册,具体如下:

1 <?php
2 use frontendassetsAppAsset;
3 AppAsset::register($this);
4 ?>
5 <?php $this->beginPage() ?>
6 <?php $this->head() ?>
7 <?php $this->beginBody() ?>
8 <?php $this->endBody() ?>
9 <?php $this->endPage() ?>

以上缺一不可。

3.添加公共模版

在相应根目录下的views目录下的layouts文件夹下创建文件main.php(默认加载,如需其他名称需在控制器内render前添加$this->layout="layout"),放入公共模版后在中间添加占位

1 <?php echo $content; ?>

4.非本地使用gii

在相应根目录下的config文件夹下的main-local文件下

1 $config['bootstrap'][] = 'gii';
2   $config['modules']['gii'] = [
3      'class' => 'yiigiiModule',
4      'allowedIPs' => ['192.168.1.1'],  
5 ];

5.添加新模块

  (1)打开gii,进入Module Generator页面,按照页面的提示进行安装。

  (2)在相应根目录下的config文件夹下的main-local文件下

 1 if (!YII_ENV_TEST) {
 2     // configuration adjustments for 'dev' environment
 3     $config['bootstrap'][] = 'debug';
 4     $config['modules']['debug'] = [
 5         'class' => 'yiidebugModule',
 6     ];
 7 
 8     $config['bootstrap'][] = 'gii';
 9     $config['modules']['gii'] = [
10         'class' => 'yiigiiModule',
11     ];
12 
13     $config['module']['admin'] = [
14         'class' => 'appmodulesadmin',
15     ];  
16 }
17 
18 return $config;

6.修改默认控制器为index(默认控制器为site)

在相应根目录下的config文件夹下的main文件下的return里,添加

1 'defaultRoute' => 'index',

7.分页

参见:http://www.yiichina.com/tutorial/774

8.

原文地址:https://www.cnblogs.com/peilanluo/p/7763204.html