Module类是模块和应用类的基类。 yiisoft\yii2\base\Module.php
<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: #000000;">use yii\di\ServiceLocator; </span><span style="color: #008080;"> 12</span> <span style="color: #008080;"> 13</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;"> 14</span> <span style="color: #008000;"> * Module is the base class for module and application classes. </span><span style="color: #008080;"> 15</span> <span style="color: #008000;"> * Module是模块和应用类的基类 </span><span style="color: #008080;"> 16</span> <span style="color: #008000;"> * A module represents a sub-application which contains MVC elements by itself, such as </span><span style="color: #008080;"> 17</span> <span style="color: #008000;"> * models, views, controllers, etc. </span><span style="color: #008080;"> 18</span> <span style="color: #008000;"> * 模块是一个由模型、视图、控制器等组成的子应用 </span><span style="color: #008080;"> 19</span> <span style="color: #008000;"> * A module may consist of [[modules|sub-modules]]. </span><span style="color: #008080;"> 20</span> <span style="color: #008000;"> * 模块内也可以包含模块或子模块 </span><span style="color: #008080;"> 21</span> <span style="color: #008000;"> * [[components|Components]] may be registered with the module so that they are globally </span><span style="color: #008080;"> 22</span> <span style="color: #008000;"> * accessible within the module. </span><span style="color: #008080;"> 23</span> <span style="color: #008000;"> * 组件可以注册到模块,以便在模块内全局访问 </span><span style="color: #008080;"> 24</span> <span style="color: #008000;"> * @property array $aliases List of path aliases to be defined. The array keys are alias names (must start </span><span style="color: #008080;"> 25</span> <span style="color: #008000;"> * with '@') and the array values are the corresponding paths or aliases. See [[setAliases()]] for an example. </span><span style="color: #008080;"> 26</span> <span style="color: #008000;"> * This property is write-only. 要定义的别名路径数组 只写 </span><span style="color: #008080;"> 27</span> <span style="color: #008000;"> * @property string $basePath The root directory of the module. 模块的根路径 </span><span style="color: #008080;"> 28</span> <span style="color: #008000;"> * @property string $controllerPath The directory that contains the controller classes. This property is </span><span style="color: #008080;"> 29</span> <span style="color: #008000;"> * read-only. 控制器类的路径 只读 </span><span style="color: #008080;"> 30</span> <span style="color: #008000;"> * @property string $layoutPath The root directory of layout files. Defaults to "[[viewPath]]/layouts". </span><span style="color: #008080;"> 31</span> <span style="color: #008000;"> * 模板路径数组 只读 </span><span style="color: #008080;"> 32</span> <span style="color: #008000;"> * @property array $modules The modules (indexed by their IDs). 模块数组 </span><span style="color: #008080;"> 33</span> <span style="color: #008000;"> * @property string $uniqueId The unique ID of the module. This property is read-only.模块的唯一标识 只读 </span><span style="color: #008080;"> 34</span> <span style="color: #008000;"> * @property string $viewPath The root directory of view files. Defaults to "[[basePath]]/views". </span><span style="color: #008080;"> 35</span> <span style="color: #008000;"> * 模块下视图文件路径 </span><span style="color: #008080;"> 36</span> <span style="color: #008000;"> * @author Qiang Xue <qiang.xue> </qiang.xue></span><span style="color: #008080;"> 37</span> <span style="color: #008000;"> * @since 2.0 </span><span style="color: #008080;"> 38</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 39</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Module extends ServiceLocator </span><span style="color: #008080;"> 40</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 41</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;"> 42</span> <span style="color: #008000;"> * @event ActionEvent an event raised before executing a controller action. 在执行控制的的action方法前触发 </span><span style="color: #008080;"> 43</span> <span style="color: #008000;"> * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. </span><span style="color: #008080;"> 44</span> <span style="color: #008000;"> * 可以设置[[ActionEvent::isValid]]为false取消行动的执行。 </span><span style="color: #008080;"> 45</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 46</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;"> 47</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;"> 48</span> <span style="color: #008000;"> * @event ActionEvent an event raised after executing a controller action. </span><span style="color: #008080;"> 49</span> <span style="color: #008000;"> * 在执行控制的的action方法后触发 </span><span style="color: #008080;"> 50</span> <span style="color: #008000;"> * </span><span style="color: #008080;"> 51</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 52</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;"> 53</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 array custom module parameters (name => value). 自定义模块参数 </span><span style="color: #008080;"> 56</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 57</span> <span style="color: #0000ff;">public</span> $<span style="color: #0000ff;">params</span> =<span style="color: #000000;"> []; </span><span style="color: #008080;"> 58</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;"> 59</span> <span style="color: #008000;"> * @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]]. </span><span style="color: #008080;"> 60</span> <span style="color: #008000;"> * 模块的唯一标识,用于区分同一父模块下的模块 </span><span style="color: #008080;"> 61</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 62</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> $id; </span><span style="color: #008080;"> 63</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;"> 64</span> <span style="color: #008000;"> * @var Module the parent module of this module. Null if this module does not have a parent. </span><span style="color: #008080;"> 65</span> <span style="color: #008000;"> * 当前模块的父模块 </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;"> $module; </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 string|boolean the layout that should be applied for views within this module. This refers to a view name </span><span style="color: #008080;"> 70</span> <span style="color: #008000;"> * relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]] </span><span style="color: #008080;"> 71</span> <span style="color: #008000;"> * will be taken. If this is false, layout will be disabled within this module. </span><span style="color: #008080;"> 72</span> <span style="color: #008000;"> * 布局文件 如果没有设置,调用 [[module|parent module]]的值。如果是false,在模块中布局将被禁用。 </span><span style="color: #008080;"> 73</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 74</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> $layout; </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 array mapping from controller ID to controller configurations. 控制器ID到控制器配置的映射 </span><span style="color: #008080;"> 77</span> <span style="color: #008000;"> * Each name-value pair specifies the configuration of a single controller. </span><span style="color: #008080;"> 78</span> <span style="color: #008000;"> * A controller configuration can be either a string or an array. </span><span style="color: #008080;"> 79</span> <span style="color: #008000;"> * If the former, the string should be the fully qualified class name of the controller. </span><span style="color: #008080;"> 80</span> <span style="color: #008000;"> * If the latter, the array must contain a 'class' element which specifies </span><span style="color: #008080;"> 81</span> <span style="color: #008000;"> * the controller's fully qualified class name, and the rest of the name-value pairs </span><span style="color: #008080;"> 82</span> <span style="color: #008000;"> * in the array are used to initialize the corresponding controller properties. For example, </span><span style="color: #008080;"> 83</span> <span style="color: #008000;"> * 每个键值对指定单独的控制器,控制器配置可以是字符串或者数组,如果是前者,该字符串是指定控制的的全路径 </span><span style="color: #008080;"> 84</span> <span style="color: #008000;"> 95 * 如果是后者,则包含一个‘class’元素指定控制器的全路径,其余的参数用于初始化对应的属性 </span><span style="color: #008080;"> 85</span> <span style="color: #008000;"> * ~~~ </span><span style="color: #008080;"> 86</span> <span style="color: #008000;"> * [ </span><span style="color: #008080;"> 87</span> <span style="color: #008000;"> * 'account' => 'app\controllers\UserController', </span><span style="color: #008080;"> 88</span> <span style="color: #008000;"> * 'article' => [ </span><span style="color: #008080;"> 89</span> <span style="color: #008000;"> * 'class' => 'app\controllers\PostController', </span><span style="color: #008080;"> 90</span> <span style="color: #008000;"> * 'pageTitle' => 'something new', </span><span style="color: #008080;"> 91</span> <span style="color: #008000;"> * ], </span><span style="color: #008080;"> 92</span> <span style="color: #008000;"> * ] </span><span style="color: #008080;"> 93</span> <span style="color: #008000;"> * ~~~ </span><span style="color: #008080;"> 94</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 95</span> <span style="color: #0000ff;">public</span> $controllerMap =<span style="color: #000000;"> []; </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;"> * @var string the namespace that controller classes are in. 控制器的命名空间 </span><span style="color: #008080;"> 98</span> <span style="color: #008000;"> * This namespace will be used to load controller classes by prepending it to the controller </span><span style="color: #008080;"> 99</span> <span style="color: #008000;"> * class name. </span><span style="color: #008080;">100</span> <span style="color: #008000;"> * 命名空间 在控制器类的前面加载控制器类 </span><span style="color: #008080;">101</span> <span style="color: #008000;"> * If not set, it will use the `controllers` sub-namespace under the namespace of this module. </span><span style="color: #008080;">102</span> <span style="color: #008000;"> * For example, if the namespace of this module is "foo\bar", then the default </span><span style="color: #008080;">103</span> <span style="color: #008000;"> * controller namespace would be "foo\bar\controllers". </span><span style="color: #008080;">104</span> <span style="color: #008000;"> * 如果没有设置,默认为当前模块的命名空间加上 `controllers`构成的命名空间 </span><span style="color: #008080;">105</span> <span style="color: #008000;">119 * 如当前模块的命名空间为"foo\bar",控制器的默认命名空间为"foo\bar\controllers" </span><span style="color: #008080;">106</span> <span style="color: #008000;"> * See also the [guide section on autoloading](guide:concept-autoloading) to learn more about </span><span style="color: #008080;">107</span> <span style="color: #008000;"> * defining namespaces and how classes are loaded. </span><span style="color: #008080;">108</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">109</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> $controllerNamespace; </span><span style="color: #008080;">110</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">111</span> <span style="color: #008000;"> * @var string the default route of this module. Defaults to 'default'. 当前前模块的默认路由 </span><span style="color: #008080;">112</span> <span style="color: #008000;"> * The route may consist of child module ID, controller ID, and/or action ID. </span><span style="color: #008080;">113</span> <span style="color: #008000;"> * For example, `help`, `post/create`, `admin/post/create`. </span><span style="color: #008080;">114</span> <span style="color: #008000;"> * If action ID is not given, it will take the default value as specified in </span><span style="color: #008080;">115</span> <span style="color: #008000;"> * [[Controller::defaultAction]]. </span><span style="color: #008080;">116</span> <span style="color: #008000;"> * route 可能包含子模块ID,控制器ID,操作ID,如果action ID未给定,会调用[Controller::defaultAction]指定的action </span><span style="color: #008080;">117</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">118</span> <span style="color: #0000ff;">public</span> $defaultRoute = <span style="color: #800000;">'</span><span style="color: #800000;">default</span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #008080;">119</span> <span style="color: #008080;">120</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">121</span> <span style="color: #008000;"> * @var string the root directory of the module. 当前模块的根路径 </span><span style="color: #008080;">122</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">123</span> <span style="color: #0000ff;">private</span><span style="color: #000000;"> $_basePath; </span><span style="color: #008080;">124</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">125</span> <span style="color: #008000;"> * @var string the root directory that contains view files for this module 当前模块下视图文件的路径 </span><span style="color: #008080;">126</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">127</span> <span style="color: #0000ff;">private</span><span style="color: #000000;"> $_viewPath; </span><span style="color: #008080;">128</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">129</span> <span style="color: #008000;"> * @var string the root directory that contains layout view files for this module. </span><span style="color: #008080;">130</span> <span style="color: #008000;"> * 当前模块下的布局文件路径 </span><span style="color: #008080;">131</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">132</span> <span style="color: #0000ff;">private</span><span style="color: #000000;"> $_layoutPath; </span><span style="color: #008080;">133</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">134</span> <span style="color: #008000;"> * @var array child modules of this module 当前模块的子模块数组 </span><span style="color: #008080;">135</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">136</span> <span style="color: #0000ff;">private</span> $_modules =<span style="color: #000000;"> []; </span><span style="color: #008080;">137</span> <span style="color: #008080;">138</span> <span style="color: #008080;">139</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">140</span> <span style="color: #008000;"> * Constructor. 构造函数 </span><span style="color: #008080;">141</span> <span style="color: #008000;"> * @param string $id the ID of this module 当前模块的标识 </span><span style="color: #008080;">142</span> <span style="color: #008000;"> * @param Module $parent the parent module (if any) 当前模块的父模块 </span><span style="color: #008080;">143</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;">144</span> <span style="color: #008000;"> * 配置文件 用于初始化对象属性 </span><span style="color: #008080;">145</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">146</span> <span style="color: #0000ff;">public</span> function __construct($id, $parent = <span style="color: #0000ff;">null</span>, $config =<span style="color: #000000;"> []) </span><span style="color: #008080;">147</span> <span style="color: #000000;"> { </span><span style="color: #008080;">148</span> $<span style="color: #0000ff;">this</span>->id = $id; <span style="color: #008000;">//</span><span style="color: #008000;">给当前模块唯一标识</span> <span style="color: #008080;">149</span> $<span style="color: #0000ff;">this</span>->module = $parent; <span style="color: #008000;">//</span><span style="color: #008000;">当前模块的父模块</span> <span style="color: #008080;">150</span> parent::__construct($config); <span style="color: #008000;">//</span><span style="color: #008000;">调用父类的配置</span> <span style="color: #008080;">151</span> <span style="color: #000000;"> } </span><span style="color: #008080;">152</span> <span style="color: #008080;">153</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">154</span> <span style="color: #008000;"> * Returns the currently requested instance of this module class. 取得当前类的实例 </span><span style="color: #008080;">155</span> <span style="color: #008000;"> * If the module class is not currently requested, null will be returned. </span><span style="color: #008080;">156</span> <span style="color: #008000;"> * 没有当前请求的模块类,将返回null。 </span><span style="color: #008080;">157</span> <span style="color: #008000;"> * This method is provided so that you access the module instance from anywhere within the module. </span><span style="color: #008080;">158</span> <span style="color: #008000;"> * 可以在模块内的任何地方访问类的实例 </span><span style="color: #008080;">159</span> <span style="color: #008000;"> * @return static|null the currently requested instance of this module class, or null if the module class is not requested. </span><span style="color: #008080;">160</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">161</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> function getInstance() </span><span style="color: #008080;">162</span> <span style="color: #000000;"> { </span><span style="color: #008080;">163</span> $<span style="color: #0000ff;">class</span> =<span style="color: #000000;"> get_called_class(); </span><span style="color: #008080;">164</span> <span style="color: #0000ff;">return</span> isset(Yii::$app->loadedModules[$<span style="color: #0000ff;">class</span>]) ? Yii::$app->loadedModules[$<span style="color: #0000ff;">class</span>] : <span style="color: #0000ff;">null</span><span style="color: #000000;">; </span><span style="color: #008080;">165</span> <span style="color: #000000;"> } </span><span style="color: #008080;">166</span> <span style="color: #008080;">167</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">168</span> <span style="color: #008000;"> * Sets the currently requested instance of this module class. 设置模块类的当前请求实例。 </span><span style="color: #008080;">169</span> <span style="color: #008000;"> * @param Module|null $instance the currently requested instance of this module class. </span><span style="color: #008080;">170</span> <span style="color: #008000;"> * If it is null, the instance of the calling class will be removed, if any. </span><span style="color: #008080;">171</span> <span style="color: #008000;"> * 当前模块类的实例。如果为null,调用类的实例将被删除 </span><span style="color: #008080;">172</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">173</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> function setInstance($instance) </span><span style="color: #008080;">174</span> <span style="color: #000000;"> { </span><span style="color: #008080;">175</span> <span style="color: #0000ff;">if</span> ($instance === <span style="color: #0000ff;">null</span>) {<span style="color: #008000;">//</span><span style="color: #008000;">如果没有传入参数,直接unset</span> <span style="color: #008080;">176</span> unset(Yii::$app-><span style="color: #000000;">loadedModules[get_called_class()]); </span><span style="color: #008080;">177</span> } <span style="color: #0000ff;">else</span> {<span style="color: #008000;">//</span><span style="color: #008000;">将该类和类的实例存入loadedModules数组中</span> <span style="color: #008080;">178</span> Yii::$app->loadedModules[get_class($instance)] =<span style="color: #000000;"> $instance; </span><span style="color: #008080;">179</span> <span style="color: #000000;"> } </span><span style="color: #008080;">180</span> <span style="color: #000000;"> } </span><span style="color: #008080;">181</span> <span style="color: #008080;">182</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">183</span> <span style="color: #008000;"> * Initializes the module. </span><span style="color: #008080;">184</span> <span style="color: #008000;"> * 初始化模块 </span><span style="color: #008080;">185</span> <span style="color: #008000;"> * This method is called after the module is created and initialized with property values </span><span style="color: #008080;">186</span> <span style="color: #008000;"> * given in configuration. The default implementation will initialize [[controllerNamespace]] </span><span style="color: #008080;">187</span> <span style="color: #008000;"> * if it is not set. </span><span style="color: #008080;">188</span> <span style="color: #008000;"> * 该模块创建和初始化给出的配置 如果没有设置,默认初始化[[controllerNamespace]] </span><span style="color: #008080;">189</span> <span style="color: #008000;"> * If you override this method, please make sure you call the parent implementation. </span><span style="color: #008080;">190</span> <span style="color: #008000;"> * 重写确保父类调用 </span><span style="color: #008080;">191</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">192</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> function init() </span><span style="color: #008080;">193</span> <span style="color: #000000;"> { </span><span style="color: #008080;">194</span> <span style="color: #0000ff;">if</span> ($<span style="color: #0000ff;">this</span>->controllerNamespace === <span style="color: #0000ff;">null</span>) {<span style="color: #008000;">//</span><span style="color: #008000;">判断是否为空</span> <span style="color: #008080;">195</span> $<span style="color: #0000ff;">class</span> = get_class($<span style="color: #0000ff;">this</span>); <span style="color: #008000;">//</span><span style="color: #008000;">获取类名</span> <span style="color: #008080;">196</span> <span style="color: #0000ff;">if</span> (($pos = strrpos($<span style="color: #0000ff;">class</span>, <span style="color: #800000;">'</span><span style="color: #800000;">\\</span><span style="color: #800000;">'</span>)) !== <span style="color: #0000ff;">false</span><span style="color: #000000;">) { </span><span style="color: #008080;">197</span> $<span style="color: #0000ff;">this</span>->controllerNamespace = substr($<span style="color: #0000ff;">class</span>, <span style="color: #800080;">0</span>, $pos) . <span style="color: #800000;">'</span><span style="color: #800000;">\\controllers</span><span style="color: #800000;">'</span>; <span style="color: #008000;">//</span><span style="color: #008000;">取得命名空间</span> <span style="color: #008080;">198</span> <span style="color: #000000;"> } </span><span style="color: #008080;">199</span> <span style="color: #000000;"> } </span><span style="color: #008080;">200</span> <span style="color: #000000;"> } </span><span style="color: #008080;">201</span> <span style="color: #008080;">202</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">203</span> <span style="color: #008000;"> * Returns an ID that uniquely identifies this module among all modules within the current application. </span><span style="color: #008080;">204</span> <span style="color: #008000;"> * Note that if the module is an application, an empty string will be returned. </span><span style="color: #008080;">205</span> <span style="color: #008000;"> * 当前应用程序中模块的唯一标识,如果该模块是应用程序返回空字符串 </span><span style="color: #008080;">206</span> <span style="color: #008000;"> * @return string the unique ID of the module.模块的唯一标识 </span><span style="color: #008080;">207</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">208</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> function getUniqueId() </span><span style="color: #008080;">209</span> { <span style="color: #008000;">//</span><span style="color: #008000;">如果当前模块有父模块,则返回拼接的标识作为唯一ID,否则只返回当前模块ID</span> <span style="color: #008080;">210</span> <span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>->module ? ltrim($<span style="color: #0000ff;">this</span>->module->getUniqueId() . <span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span> . $<span style="color: #0000ff;">this</span>->id, <span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span>) : $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">id; </span><span style="color: #008080;">211</span> <span style="color: #000000;"> } </span><span style="color: #008080;">212</span> <span style="color: #008080;">213</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">214</span> <span style="color: #008000;"> * Returns the root directory of the module. 返回当前模块的根路径 </span><span style="color: #008080;">215</span> <span style="color: #008000;"> * It defaults to the directory containing the module class file. 默认为包含模块类文件的路径。 </span><span style="color: #008080;">216</span> <span style="color: #008000;"> * @return string the root directory of the module. 当前模块的根路径 </span><span style="color: #008080;">217</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">218</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> function getBasePath() </span><span style="color: #008080;">219</span> <span style="color: #000000;"> { </span><span style="color: #008080;">220</span> <span style="color: #0000ff;">if</span> ($<span style="color: #0000ff;">this</span>->_basePath === <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #008080;">221</span> $<span style="color: #0000ff;">class</span> = <span style="color: #0000ff;">new</span> \ReflectionClass($<span style="color: #0000ff;">this</span>); <span style="color: #008000;">//</span><span style="color: #008000;">生成当前类的反射对象</span> <span style="color: #008080;">222</span> $<span style="color: #0000ff;">this</span>->_basePath = dirname($<span style="color: #0000ff;">class</span>->getFileName());<span style="color: #008000;">//</span><span style="color: #008000;">取得类定义的路径</span> <span style="color: #008080;">223</span> <span style="color: #000000;"> } </span><span style="color: #008080;">224</span> <span style="color: #008080;">225</span> <span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">_basePath; </span><span style="color: #008080;">226</span> <span style="color: #000000;"> } </span><span style="color: #008080;">227</span> <span style="color: #008080;">228</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008080;">229</span> <span style="color: #008000;"> * Sets the root directory of the module. 设置当前模块的根路径 </span><span style="color: #008080;">230</span> <span style="color: #008000;"> * This method can only be invoked at the beginning of the constructor. 只在构造函数开始时调用。 </span><span style="color: #008080;">231</span> <span style="color: #008000;"> * @param string $path the root directory of the module. This can be either a directory name or a path alias. </span><span style="color: #008080;">232</span> <span style="color: #008000;"> * 模块的根目录。可以是一个目录名或路径别名 </span><span style="color: #008080;">233</span> <span style="color: #008000;"> * @throws InvalidParamException if the directory does not exist. 如果路径不存在。抛出异常 </span><span style="color: #008080;">234</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">235</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> function setBasePath($path) </span><span style="color: #008080;">236</span> <span style="color: #000000;"> { </span><span style="color: #008080;">237</span> $path = Yii::getAlias($path);<span style="color: #008000;">//</span><span style="color: #008000;">将路径别名转换为实际路径。</span> <span style="color: #008080;">238</span> $p = realpath($path); <span style="color: #008000;">//</span><span style="color: #008000;">返回绝对路径名</span> <span style="color: #008080;">239</span> <span style="color: #0000ff;">if</span> ($p !== <span style="color: #0000ff;">false</span> &&<span style="color: #000000;"> is_dir($p)) { </span><span style="color: #008080;">240</span> $<span style="color: #0000ff;">this</span>->_basePath = $p;<span style="color: #008000;">//</span><span style="color: #008000;">是目录名且不为false,返回目录名,否则抛出异常</span> <span style="color: #008080;">241</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">242</span> <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> InvalidParamException(<span style="color: #800000;">"</span><span style="color: #800000;">The directory does not exist: $path</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">243</span> <span style="color: #000000;"> } </span><span style="color: #008080;">244</span> }