fly020212
- fly020212 回答了问题 2.0版本中如何取得当前的控制器和方法
在 控制器 里面
var_dump($this->id, $this->action->id);
在 视图 里面
var_dump($this->context->id, $this->context->action->id);
- fly020212 回答了问题 Yii2.0 高级模板入口文件
默认是没有入口文件的,需要你用
init
脚本初始化 - fly020212 收藏了话题页面缓存如何依赖GET参数
直接
$model->load(Yii::$app->request->post())
或者$model->attr = Yii::$app->request->post('attrArray')
,model
里面就可以使用了,如果model
不方便保存属性的话,也可以用Yii::$app->request->post('attrArray')
在model
里面直接访问,不清楚是不是你想要的结果
/** * @param string $id the ID of this controller. * @param Module $module the module that this controller belongs to. * @param array $config name-value pairs that will be used to initialize the object properties. */ public function __construct($id, $module, $config = []) { $this->id = $id; $this->module = $module; parent::__construct($config); }
yii
框架需要自己创建Controller
的实例,你任何时候都不需要覆盖人家的构造函数
,如果你需要覆盖的话,也需要和它的方法签名
一样,并且调用parent::__construct
,99.999%
的情况下,你不需要这么做你的问题是,在
controler
初始化的时候,做些验证,用init()
方法就可以了$config['components']['gii']
这个位置config 目录下,组件配置的文件
- fly020212 评论了教程 Yii 操作 Session
yii2 下
如果使用
yii\web\User
的logout
方法的话,默认配置下是会把 session destroy 的/** * Logs out the current user. * This will remove authentication-related session data. * If `$destroySession` is true, all session data will be removed. * @param boolean $destroySession whether to destroy the whole session. Defaults to true. * This parameter is ignored if [[enableSession]] is false. * @return boolean whether the user is logged out */ public function logout($destroySession = true) { $identity = $this->getIdentity(); if ($identity !== null && $this->beforeLogout($identity)) { $this->switchIdentity(null); $id = $identity->getId(); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged out from $ip.", __METHOD__); if ($destroySession && $this->enableSession) { Yii::$app->getSession()->destroy(); } $this->afterLogout($identity); } return $this->getIsGuest(); }
- fly020212 回答了问题 yii2基类Closure
这个是 php 的基础类型
http://php.net/manual/en/class.closure.php
Response
是有format
设置的,默认是const FORMAT_HTML = 'html'
,Yii2
里面,action
方法可以返回任何类型,但最终会被处理为Response
类型,在处理输出的过程中,会根据format
来设置Content-Type
类型,比如你返回数组,如果此时设置了format
为json
,则返回json
类型,如果format
设置为xml
,则返回xml
类型上面的问题,估计是你没有设置
format
属性,并且action
返回了字符串,所以被当成了text/html
处理了,可以把format
设置为const FORMAT_RAW = 'raw'
,看下ok不。其实
Response
已经有方法能处理文件类型的,public function sendFile($filePath, $attachmentName = null, $options = [])
, 这个可以自己研究下,我没有测试,只是在源代码中看到的