yii2源码学习笔记(十一)
Controller控制器类,是所有控制器的基类,用于调用模型和布局。
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/ namespace yii\base; use Yii; /**
* Controller is the base class for classes containing controller logic.
* 控制器,是所用控制器类的基类
* @property Module[] $modules All ancestor modules that this controller is located within. This property is
* read-only.只读属性 当前控制器的所有模块
* @property string $route The route (module ID, controller ID and action ID) of the current request. This
* property is read-only.当前请求的路径 只读属性 可以获取到请求的路径
* @property string $uniqueId The controller ID that is prefixed with the module ID (if any). This property is
* read-only.为前缀的controller ID 唯一标识
* @property View|\yii\web\View $view The view object that can be used to render views or view files.
* 视图用来传递视图或视图文件.
* @property string $viewPath The directory containing the view files for this controller. This property is
* read-only. 包含当前控制器的视图目录
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Controller extends Component implements ViewContextInterface
{
/**
* @event ActionEvent an event raised right before executing a controller action.
* ActionEvent事件提出正确的执行器动作之前执行。
* You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
* 如果对事件的isValid属性设置为false,将取消action的执行
*/
const EVENT_BEFORE_ACTION = 'beforeAction';
/**
* @event ActionEvent an event raised right after executing a controller action.
* 在执行controller操作后触发的事件
*/
const EVENT_AFTER_ACTION = 'afterAction'; /**
* @var string the ID of this controller.
* 控制器id
*/
public $id;
/**
* @var Module $module the module that this controller belongs to.
* 所属模块
*/
public $module;
/**
* @var string the ID of the action that is used when the action ID is not specified
* in the request. Defaults to 'index'.控制器中默认动作,默认为index
*/
public $defaultAction = 'index';
/**
* @var string|boolean the name of the layout to be applied to this controller's views.
* 布局的名称 应用到该控制器的视图。
* This property mainly affects the behavior of [[render()]].此属性主要影响[[render()]]行为
* Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value.
* If false, no layout will be applied.
* 如果设置为false,则不使用布局文件
*/
public $layout;
/**
* @var Action the action that is currently being executed. This property will be set
* by [[run()]] when it is called by [[Application]] to run an action.
* 当前执行的操作,可在事件中根据这个action来执行不同的操作
*/
public $action; /**
* @var View the view object that can be used to render views or view files.
* 视图对象,用来定义输出的视图文件
*/
private $_view; /**
* @param string $id the ID of this controller.控制器的ID
* @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 = [])
{
//初始化控制器id,模块,根据配置文件初始化控制器对象
$this->id = $id;
$this->module = $module;
parent::__construct($config);
} /**
* Declares external actions for the controller.定义action声明控制器的外部操作
* This method is meant to be overwritten to declare external actions for the controller.
* It should return an array, with array keys being action IDs, and array values the corresponding
* action class names or action configuration arrays. For example,
* 这个方法指定独立的action,返回格式为数组,name为action的id,value为action类的实现,
* ~~~
* return [
* 'action1' => 'app\components\Action1',
* 'action2' => [
* 'class' => 'app\components\Action2',
* 'property1' => 'value1',
* 'property2' => 'value2',
* ],
* ];
* ~~~
*
* [[\Yii::createObject()]] will be used later to create the requested action
* using the configuration provided here.
* 使用此处提供的配置来创建请求的操作。
*/
public function actions()
{
return [];
} /**
* Runs an action within this controller with the specified action ID and parameters.
* 控制器中运行指定的操作标识和参数。
* If the action ID is empty, the method will use [[defaultAction]].
* 如果没有定义ID,会调用默认操作
* @param string $id the ID of the action to be executed. 要执行的动作标识。
* @param array $params the parameters (name-value pairs) to be passed to the action.
* 传递给操作的参数。
* @return mixed the result of the action. 操作结果
* @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
* @see createAction()
*/
public function runAction($id, $params = [])
{
$action = $this->createAction($id);//创建操作
if ($action === null) {//创建失败,抛出异常
throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id);
} Yii::trace("Route to run: " . $action->getUniqueId(), __METHOD__); if (Yii::$app->requestedAction === null) {
// 记录当前的操作为requestedAction
Yii::$app->requestedAction = $action;
} $oldAction = $this->action;//将操作中的信息保存
$this->action = $action;//写入属性
//保存当前控制器的所有父模块
$modules = [];
$runAction = true; // call beforeAction on modules 从外到里一层层执行module的beforeAction
foreach ($this->getModules() as $module) {
if ($module->beforeAction($action)) {
// 将执行成功的module放入到$modules中,顺序会颠倒
array_unshift($modules, $module);
} else {
// 执行失败,就标记一下
$runAction = false;
break;
}
} $result = null; if ($runAction && $this->beforeAction($action)) {
// run the action 执行成功就执行action
$result = $action->runWithParams($params);
// 执行controller本身的afterAction
$result = $this->afterAction($action, $result); // call afterAction on modules 从里到外一层层执行所有
foreach ($modules as $module) {
/* @var $module Module */
$result = $module->afterAction($action, $result);
}
} $this->action = $oldAction; return $result;
}
yii2\base\Controller.php
yii2源码学习笔记(十一)的更多相关文章
- yii2源码学习笔记(九)
Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...
- yii2源码学习笔记(八)
Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...
- 老刘 Yii2 源码学习笔记之 Action 类
Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...
- yii2源码学习笔记(二十)
Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...
- yii2源码学习笔记(十八)
View继承了component,用于渲染视图文件:yii2\base\View.php <?php /** * @link http://www.yiiframework.com/ * @co ...
- yii2源码学习笔记(十七)
Theme 类,应用的主题,通过替换路径实现主题的应用,方法为获取根路径和根链接:yii2\base\Theme.php <?php /** * @link http://www.yiifram ...
- yii2源码学习笔记(十四)
Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...
- yii2源码学习笔记(十三)
模型类DynamicModel主要用于实现模型内的数据验证yii2\base\DynamicModel.php <?php /** * @link http://www.yiiframework ...
- yii2源码学习笔记(六)
Behvaior类,Behavior类是所有事件类的基类: 目录yii2\base\Behavior.php <?php /** * @link http://www.yiiframework. ...
随机推荐
- [置顶] 程序员必知(三):一分钟知道URI编码(encodeURI)
因为浏览器会用一些特殊的字符作为特定的意义,所以在要传输的内容上如果有这些特殊的字符的话,就需要对其进行转义才能正确传输,如以下字符为发送时候的关键字,即特殊字符 ;/?:@&=+$,# 所以 ...
- Kinect for Windows SDK开发入门(一):开发环境配置
[译]Kinect for Windows SDK开发入门(一):开发环境配置 前几天无意中看到微软发布了Kinect for windows sensor,进去看了一下Kinect应用的例子,发现K ...
- Nodejs实现代理服务器配置
var net = require('net'); var local_port = 8893; //在本地创建一个server监听本地local_port端口 net.createServer(fu ...
- Redis集群方案应该怎么做
方案1:Redis官方集群方案 Redis Cluster Redis Cluster是一种服务器sharding分片技术.Redis Cluster集群如何搭建请参考我的另一篇博文:http://w ...
- CentOS 修改IP地址, DNS, 网关
一.CentOS 修改IP地址 修改对应网卡的IP地址的配置文件# vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改以下内容DEVICE=eth0 #描述 ...
- Vim 快捷键整理
一.移动光标 1.左移h.右移l.下移j.上移k 2.向下翻页ctrl + f,向上翻页ctrl + b 3.向下翻半页ctrl + d,向上翻半页ctrl + u 4.移动到行尾$,移动到行首0(数 ...
- Redis作者谈Redis应用场景
Redis作者谈Redis应用场景 毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多 ...
- [置顶] [VS2010]逸雨清风 永久稳定音乐外链生成软件V0.1
音乐外链说明:现在的很多网站都有用到外链,特别是音乐外链,在博客.空间里设作背景音乐.网上也有很多上传外链的网站,不过都不稳定而且有容量限制,而且似乎所有网站其实都是用的同一个源码组件,都是链接到Ra ...
- C# - 系统类 - Object类
Object类 ns:System 此类是所有.NET Framework中的类的基类 Type类就派生自Object类 C#提供了object关键字来表示一个类实例的类型 而无需使用Object作为 ...
- 解决DataTable中的DataColumn类型默认为int类型时, 导致不能修改其列值为其他类型的解决办法
问题起因: 扔给数据库一条select * from [表名] , 得到一个DataTable, 发现有一列status状态的DataColumn的类型是int,然后我想换成字典表里的文字描述,然后就 ...