Heim >php教程 >php手册 >yii2源码学习笔记(十一)

yii2源码学习笔记(十一)

WBOY
WBOYOriginal
2016-06-06 09:32:371158Durchsuche

Controller控制器类,是所有控制器的基类,用于调用模型和布局。

<span style="color: #008080;">  1</span> <span style="color: #000000;">php
</span><span style="color: #008080;">  2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">  3</span> <span style="color: #008000;"> * @link </span><span style="color: #008000; text-decoration: underline;">http://www.yiiframework.com/</span>
<span style="color: #008080;">  4</span> <span style="color: #008000;"> * @copyright Copyright (c) 2008 Yii Software LLC
</span><span style="color: #008080;">  5</span> <span style="color: #008000;"> * @license </span><span style="color: #008000; text-decoration: underline;">http://www.yiiframework.com/license/</span>
<span style="color: #008080;">  6</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;">  7</span> 
<span style="color: #008080;">  8</span> <span style="color: #0000ff;">namespace</span> yii\<span style="color: #0000ff;">base</span><span style="color: #000000;">;
</span><span style="color: #008080;">  9</span> 
<span style="color: #008080;"> 10</span> <span style="color: #000000;">use Yii;
</span><span style="color: #008080;"> 11</span> 
<span style="color: #008080;"> 12</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 13</span> <span style="color: #008000;"> * Controller is the base class for classes containing controller logic.
</span><span style="color: #008080;"> 14</span> <span style="color: #008000;"> *  控制器,是所用控制器类的基类
</span><span style="color: #008080;"> 15</span> <span style="color: #008000;"> * @property Module[] $modules All ancestor modules that this controller is located within. This property is
</span><span style="color: #008080;"> 16</span> <span style="color: #008000;"> * read-only.只读属性  当前控制器的所有模块
</span><span style="color: #008080;"> 17</span> <span style="color: #008000;"> * @property string $route The route (module ID, controller ID and action ID) of the current request. This
</span><span style="color: #008080;"> 18</span> <span style="color: #008000;"> * property is read-only.当前请求的路径  只读属性 可以获取到请求的路径
</span><span style="color: #008080;"> 19</span> <span style="color: #008000;"> * @property string $uniqueId The controller ID that is prefixed with the module ID (if any). This property is
</span><span style="color: #008080;"> 20</span> <span style="color: #008000;"> * read-only.为前缀的controller ID  唯一标识
</span><span style="color: #008080;"> 21</span> <span style="color: #008000;"> * @property View|\yii\web\View $view The view object that can be used to render views or view files.
</span><span style="color: #008080;"> 22</span> <span style="color: #008000;"> * 视图用来传递视图或视图文件.
</span><span style="color: #008080;"> 23</span> <span style="color: #008000;"> * @property string $viewPath The directory containing the view files for this controller. This property is
</span><span style="color: #008080;"> 24</span> <span style="color: #008000;"> * read-only. 包含当前控制器的视图目录
</span><span style="color: #008080;"> 25</span> <span style="color: #008000;"> *
</span><span style="color: #008080;"> 26</span> <span style="color: #008000;"> * @author Qiang Xue <qiang.xue>
</qiang.xue></span><span style="color: #008080;"> 27</span> <span style="color: #008000;"> * @since 2.0
</span><span style="color: #008080;"> 28</span>  <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 29</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Controller extends Component implements ViewContextInterface
</span><span style="color: #008080;"> 30</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 31</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 32</span> <span style="color: #008000;">     * @event ActionEvent an event raised right before executing a controller action.
</span><span style="color: #008080;"> 33</span> <span style="color: #008000;">     * ActionEvent事件提出正确的执行器动作之前执行。
</span><span style="color: #008080;"> 34</span> <span style="color: #008000;">     * You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
</span><span style="color: #008080;"> 35</span> <span style="color: #008000;">     * 如果对事件的isValid属性设置为false,将取消action的执行
</span><span style="color: #008080;"> 36</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 37</span>     <span style="color: #0000ff;">const</span> EVENT_BEFORE_ACTION = <span style="color: #800000;">'</span><span style="color: #800000;">beforeAction</span><span style="color: #800000;">'</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 38</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 39</span> <span style="color: #008000;">     * @event ActionEvent an event raised right after executing a controller action.
</span><span style="color: #008080;"> 40</span> <span style="color: #008000;">     * 在执行controller操作后触发的事件
</span><span style="color: #008080;"> 41</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 42</span>     <span style="color: #0000ff;">const</span> EVENT_AFTER_ACTION = <span style="color: #800000;">'</span><span style="color: #800000;">afterAction</span><span style="color: #800000;">'</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 43</span> 
<span style="color: #008080;"> 44</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 45</span> <span style="color: #008000;">     * @var string the ID of this controller.
</span><span style="color: #008080;"> 46</span> <span style="color: #008000;">     * 控制器id
</span><span style="color: #008080;"> 47</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 48</span>     <span style="color: #0000ff;">public</span><span style="color: #000000;"> $id;
</span><span style="color: #008080;"> 49</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 50</span> <span style="color: #008000;">     * @var Module $module the module that this controller belongs to.
</span><span style="color: #008080;"> 51</span> <span style="color: #008000;">     * 所属模块
</span><span style="color: #008080;"> 52</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 53</span>     <span style="color: #0000ff;">public</span><span style="color: #000000;"> $module;
</span><span style="color: #008080;"> 54</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 55</span> <span style="color: #008000;">     * @var string the ID of the action that is used when the action ID is not specified
</span><span style="color: #008080;"> 56</span> <span style="color: #008000;">     * in the request. Defaults to 'index'.控制器中默认动作,默认为index
</span><span style="color: #008080;"> 57</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 58</span>     <span style="color: #0000ff;">public</span> $defaultAction = <span style="color: #800000;">'</span><span style="color: #800000;">index</span><span style="color: #800000;">'</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 59</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 60</span> <span style="color: #008000;">     * @var string|boolean the name of the layout to be applied to this controller's views.
</span><span style="color: #008080;"> 61</span> <span style="color: #008000;">     * 布局的名称 应用到该控制器的视图。
</span><span style="color: #008080;"> 62</span> <span style="color: #008000;">     * This property mainly affects the behavior of [[render()]].此属性主要影响[[render()]]行为
</span><span style="color: #008080;"> 63</span> <span style="color: #008000;">     * Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value.
</span><span style="color: #008080;"> 64</span> <span style="color: #008000;">     * If false, no layout will be applied. 
</span><span style="color: #008080;"> 65</span> <span style="color: #008000;">     * 如果设置为false,则不使用布局文件
</span><span style="color: #008080;"> 66</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 67</span>     <span style="color: #0000ff;">public</span><span style="color: #000000;"> $layout;
</span><span style="color: #008080;"> 68</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 69</span> <span style="color: #008000;">     * @var Action the action that is currently being executed. This property will be set
</span><span style="color: #008080;"> 70</span> <span style="color: #008000;">     * by [[run()]] when it is called by [[Application]] to run an action.
</span><span style="color: #008080;"> 71</span> <span style="color: #008000;">     * 当前执行的操作,可在事件中根据这个action来执行不同的操作
</span><span style="color: #008080;"> 72</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 73</span>     <span style="color: #0000ff;">public</span><span style="color: #000000;"> $action;
</span><span style="color: #008080;"> 74</span> 
<span style="color: #008080;"> 75</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 76</span> <span style="color: #008000;">     * @var View the view object that can be used to render views or view files.
</span><span style="color: #008080;"> 77</span> <span style="color: #008000;">     * 视图对象,用来定义输出的视图文件
</span><span style="color: #008080;"> 78</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 79</span>     <span style="color: #0000ff;">private</span><span style="color: #000000;"> $_view;
</span><span style="color: #008080;"> 80</span> 
<span style="color: #008080;"> 81</span> 
<span style="color: #008080;"> 82</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 83</span> <span style="color: #008000;">     * @param string $id the ID of this controller.控制器的ID
</span><span style="color: #008080;"> 84</span> <span style="color: #008000;">     * @param Module $module the module that this controller belongs to.控制器的模块
</span><span style="color: #008080;"> 85</span> <span style="color: #008000;">     * @param array $config name-value pairs that will be used to initialize the object properties.
</span><span style="color: #008080;"> 86</span> <span style="color: #008000;">     * 初始化对像时的配置文件
</span><span style="color: #008080;"> 87</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 88</span>     <span style="color: #0000ff;">public</span> function __construct($id, $module, $config =<span style="color: #000000;"> [])
</span><span style="color: #008080;"> 89</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 90</span>         <span style="color: #008000;">//</span><span style="color: #008000;">初始化控制器id,模块,根据配置文件初始化控制器对象</span>
<span style="color: #008080;"> 91</span>         $<span style="color: #0000ff;">this</span>->id =<span style="color: #000000;"> $id;
</span><span style="color: #008080;"> 92</span>         $<span style="color: #0000ff;">this</span>->module =<span style="color: #000000;"> $module;
</span><span style="color: #008080;"> 93</span> <span style="color: #000000;">        parent::__construct($config);
</span><span style="color: #008080;"> 94</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 95</span> 
<span style="color: #008080;"> 96</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 97</span> <span style="color: #008000;">     * Declares external actions for the controller.定义action声明控制器的外部操作
</span><span style="color: #008080;"> 98</span> <span style="color: #008000;">     * This method is meant to be overwritten to declare external actions for the controller.
</span><span style="color: #008080;"> 99</span> <span style="color: #008000;">     * It should return an array, with array keys being action IDs, and array values the corresponding
</span><span style="color: #008080;">100</span> <span style="color: #008000;">     * action class names or action configuration arrays. For example,
</span><span style="color: #008080;">101</span> <span style="color: #008000;">     * 这个方法指定独立的action,返回格式为数组,name为action的id,value为action类的实现,
</span><span style="color: #008080;">102</span> <span style="color: #008000;">     * ~~~
</span><span style="color: #008080;">103</span> <span style="color: #008000;">     * return [
</span><span style="color: #008080;">104</span> <span style="color: #008000;">     *     'action1' => 'app\components\Action1',
</span><span style="color: #008080;">105</span> <span style="color: #008000;">     *     'action2' => [
</span><span style="color: #008080;">106</span> <span style="color: #008000;">     *         'class' => 'app\components\Action2',
</span><span style="color: #008080;">107</span> <span style="color: #008000;">     *         'property1' => 'value1',
</span><span style="color: #008080;">108</span> <span style="color: #008000;">     *         'property2' => 'value2',
</span><span style="color: #008080;">109</span> <span style="color: #008000;">     *     ],
</span><span style="color: #008080;">110</span> <span style="color: #008000;">     * ];
</span><span style="color: #008080;">111</span> <span style="color: #008000;">     * ~~~
</span><span style="color: #008080;">112</span> <span style="color: #008000;">     *
</span><span style="color: #008080;">113</span> <span style="color: #008000;">     * [[\Yii::createObject()]] will be used later to create the requested action
</span><span style="color: #008080;">114</span> <span style="color: #008000;">     * using the configuration provided here.
</span><span style="color: #008080;">115</span> <span style="color: #008000;">     * 使用此处提供的配置来创建请求的操作。
</span><span style="color: #008080;">116</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">117</span>     <span style="color: #0000ff;">public</span><span style="color: #000000;"> function actions()
</span><span style="color: #008080;">118</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">119</span>         <span style="color: #0000ff;">return</span><span style="color: #000000;"> [];
</span><span style="color: #008080;">120</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">121</span> 
<span style="color: #008080;">122</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;">123</span> <span style="color: #008000;">     * Runs an action within this controller with the specified action ID and parameters.
</span><span style="color: #008080;">124</span> <span style="color: #008000;">     * 控制器中运行指定的操作标识和参数。
</span><span style="color: #008080;">125</span> <span style="color: #008000;">     * If the action ID is empty, the method will use [[defaultAction]].
</span><span style="color: #008080;">126</span> <span style="color: #008000;">     * 如果没有定义ID,会调用默认操作
</span><span style="color: #008080;">127</span> <span style="color: #008000;">     * @param string $id the ID of the action to be executed. 要执行的动作标识。
</span><span style="color: #008080;">128</span> <span style="color: #008000;">     * @param array $params the parameters (name-value pairs) to be passed to the action.
</span><span style="color: #008080;">129</span> <span style="color: #008000;">     * 传递给操作的参数。
</span><span style="color: #008080;">130</span> <span style="color: #008000;">     * @return mixed the result of the action.  操作结果
</span><span style="color: #008080;">131</span> <span style="color: #008000;">     * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
</span><span style="color: #008080;">132</span> <span style="color: #008000;">     * @see createAction()
</span><span style="color: #008080;">133</span>      <span style="color: #008000;">*/</span>
<span style="color: #008080;">134</span>     <span style="color: #0000ff;">public</span> function runAction($id, $<span style="color: #0000ff;">params</span> =<span style="color: #000000;"> [])
</span><span style="color: #008080;">135</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">136</span>         $action = $<span style="color: #0000ff;">this</span>->createAction($id);<span style="color: #008000;">//</span><span style="color: #008000;">创建操作</span>
<span style="color: #008080;">137</span>         <span style="color: #0000ff;">if</span> ($action === <span style="color: #0000ff;">null</span>) {<span style="color: #008000;">//</span><span style="color: #008000;">创建失败,抛出异常</span>
<span style="color: #008080;">138</span>             <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> InvalidRouteException(<span style="color: #800000;">'</span><span style="color: #800000;">Unable to resolve the request: </span><span style="color: #800000;">'</span> . $<span style="color: #0000ff;">this</span>->getUniqueId() . <span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span><span style="color: #000000;"> . $id);
</span><span style="color: #008080;">139</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">140</span> 
<span style="color: #008080;">141</span>         Yii::trace(<span style="color: #800000;">"</span><span style="color: #800000;">Route to run: </span><span style="color: #800000;">"</span> . $action-><span style="color: #000000;">getUniqueId(), __METHOD__);
</span><span style="color: #008080;">142</span> 
<span style="color: #008080;">143</span>         <span style="color: #0000ff;">if</span> (Yii::$app->requestedAction === <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #008080;">144</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 记录当前的操作为requestedAction</span>
<span style="color: #008080;">145</span>             Yii::$app->requestedAction =<span style="color: #000000;"> $action;
</span><span style="color: #008080;">146</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">147</span> 
<span style="color: #008080;">148</span>         $oldAction = $<span style="color: #0000ff;">this</span>->action;<span style="color: #008000;">//</span><span style="color: #008000;">将操作中的信息保存</span>
<span style="color: #008080;">149</span>         $<span style="color: #0000ff;">this</span>->action = $action;<span style="color: #008000;">//</span><span style="color: #008000;">写入属性
</span><span style="color: #008080;">150</span>         <span style="color: #008000;">//</span><span style="color: #008000;">保存当前控制器的所有父模块</span>
<span style="color: #008080;">151</span>         $modules =<span style="color: #000000;"> [];
</span><span style="color: #008080;">152</span>         $runAction = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">153</span> 
<span style="color: #008080;">154</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> call beforeAction on modules 从外到里一层层执行module的beforeAction</span>
<span style="color: #008080;">155</span>         <span style="color: #0000ff;">foreach</span> ($<span style="color: #0000ff;">this</span>->getModules() <span style="color: #0000ff;">as</span><span style="color: #000000;"> $module) {
</span><span style="color: #008080;">156</span>             <span style="color: #0000ff;">if</span> ($module-><span style="color: #000000;">beforeAction($action)) {
</span><span style="color: #008080;">157</span>                  <span style="color: #008000;">//</span><span style="color: #008000;"> 将执行成功的module放入到$modules中,顺序会颠倒</span>
<span style="color: #008080;">158</span> <span style="color: #000000;">                array_unshift($modules, $module);
</span><span style="color: #008080;">159</span>             } <span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;">160</span>                  <span style="color: #008000;">//</span><span style="color: #008000;"> 执行失败,就标记一下</span>
<span style="color: #008080;">161</span>                 $runAction = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">162</span>                 <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">163</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">164</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">165</span> 
<span style="color: #008080;">166</span>         $result = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">167</span> 
<span style="color: #008080;">168</span>         <span style="color: #0000ff;">if</span> ($runAction && $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">beforeAction($action)) {
</span><span style="color: #008080;">169</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> run the action 执行成功就执行action</span>
<span style="color: #008080;">170</span>             $result = $action->runWithParams($<span style="color: #0000ff;">params</span><span style="color: #000000;">);
</span><span style="color: #008080;">171</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 执行controller本身的afterAction</span>
<span style="color: #008080;">172</span>             $result = $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">afterAction($action, $result);
</span><span style="color: #008080;">173</span> 
<span style="color: #008080;">174</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> call afterAction on modules 从里到外一层层执行所有</span>
<span style="color: #008080;">175</span>             <span style="color: #0000ff;">foreach</span> ($modules <span style="color: #0000ff;">as</span><span style="color: #000000;"> $module) {
</span><span style="color: #008080;">176</span>                 <span style="color: #008000;">/*</span><span style="color: #008000;"> @var $module Module </span><span style="color: #008000;">*/</span>
<span style="color: #008080;">177</span>                 $result = $module-><span style="color: #000000;">afterAction($action, $result);
</span><span style="color: #008080;">178</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">179</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">180</span> 
<span style="color: #008080;">181</span>         $<span style="color: #0000ff;">this</span>->action =<span style="color: #000000;"> $oldAction;
</span><span style="color: #008080;">182</span> 
<span style="color: #008080;">183</span>         <span style="color: #0000ff;">return</span><span style="color: #000000;"> $result;
</span><span style="color: #008080;">184</span>     }

 

yii2\base\Controller.php

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn