隐藏index.php文件
目的:想去掉浏览器地址栏中的 index.php?r= 这一块。
在/config/web.php中 ’components'=>[] 中添加如下代码:
1 'urlManager' => [ 2 'enablePrettyUrl' => true,//路由的路径化 3 'enableStrictParsing' => false,//禁止严格url解析 4 'showScriptName' => false,//允许去掉index.php 5 'rules' => [ 6 //['class' => 'yii estUrlRule', 'controller' => 'test'], 7 ], 8 ],
改了以上这些,?r= 可以用 / 代替访问了,但是想隐藏掉index.php还是不行。
需要在/web目录中,index.php同级的目录下添加.htaccess文件。
新建htaccess文件代码示例:
1 Options +FollowSymLinks 2 IndexIgnore / 3 RewriteEngine on 4 RewriteCond %{REQUEST_FILENAME} !-f 5 RewriteCond %{REQUEST_FILENAME} !-d 6 RewriteRule . index.php
模块配置
创建模块,该目录中有子目录如Module.php, controllers,models,views 分别为对应模块类文件,控制器,模型,视图等。
一个模块类文件,demo大致定义:
1 <?php 2 3 namespace appmodulesadmin; 4 5 /** 6 * admin module definition class 7 */ 8 class Module extends yiiaseModule 9 { 10 /** 11 * {@inheritdoc} 12 */ 13 public $controllerNamespace = 'appmodulesadmincontrollers'; 14 15 /** 16 * {@inheritdoc} 17 */ 18 public function init() 19 { 20 parent::init(); 21 // custom initialization code goes here 22 } 23 }
在/config/web.php中添加模块配置:
$config = [ 'modules' => [ 'admin' => [ 'class' => 'appmodulesadminModule', 'defaultRoute' => 'index', ], ], ]
视图 layout 布局配置和禁用
使用yii2下载安装包,加载视图会自动加载yii界面默认布局。自定义配置和禁用方法如下:
方案1:控制器内成员变量
public $layout = false; //不使用布局 public $layout = "main"; //设置使用的布局文件
方案2:控制器成员方法内
$this->layout = false; //不使用布局 $this->layout = "main"; //设置使用的布局文件
方案3:视图中选择布局
$this->context->layout = false; //不使用布局
$this->context->layout = 'main'; //设置使用的布局文件
yii2.0 日志写入
使用方法:
1、Yii::getLogger()->log($message, $level, $category = 'application'); 2、Yii::trace($message, $category = 'application'); 3、Yii::error($message, $category = 'application'); 4、Yii::warning($message, $category = 'application'); 5、Yii::info($message, $category = 'application;);
在/config/web.php中修改log配置,demo如下:
$config = [ 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ //可以配置多个log [ 'class' => 'yiilogFileTarget', //yii2处理日志的类 'levels' => ['error', 'warning','info','trace'], //设置日志记录的级别 'logVars' => ['*'], //捕获请求参数 //'categories' => ['application'], //自定义日志分类 //'logFile' => '@runtime/logs/app.log', //自定义文件路径 ], [ 'class' => 'yiilogFileTarget', 'levels' => ['error', 'warning','info','trace'], 'logVars' => ['*'], 'categories' => ['test'], 'logFile' => '@runtime/logs/test.log', ] ], ], ]