CWebModule.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * CWebModule class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CWebModule represents an application module.
  12. *
  13. * An application module may be considered as a self-contained sub-application
  14. * that has its own controllers, models and views and can be reused in a different
  15. * project as a whole. Controllers inside a module must be accessed with routes
  16. * that are prefixed with the module ID.
  17. *
  18. * @property string $name The name of this module.
  19. * @property string $description The description of this module.
  20. * @property string $version The version of this module.
  21. * @property string $controllerPath The directory that contains the controller classes. Defaults to 'moduleDir/controllers'
  22. * where moduleDir is the directory containing the module class.
  23. * @property string $viewPath The root directory of view files. Defaults to 'moduleDir/views' where moduleDir is
  24. * the directory containing the module class.
  25. * @property string $layoutPath The root directory of layout files. Defaults to 'moduleDir/views/layouts' where
  26. * moduleDir is the directory containing the module class.
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @package system.web
  30. */
  31. class CWebModule extends CModule
  32. {
  33. /**
  34. * @var string the ID of the default controller for this module. Defaults to 'default'.
  35. */
  36. public $defaultController='default';
  37. /**
  38. * @var mixed the layout that is shared by the controllers inside this module.
  39. * If a controller has explicitly declared its own {@link CController::layout layout},
  40. * this property will be ignored.
  41. * If this is null (default), the application's layout or the parent module's layout (if available)
  42. * will be used. If this is false, then no layout will be used.
  43. */
  44. public $layout;
  45. /**
  46. * @var string Namespace that should be used when loading controllers.
  47. * Default is to use global namespace.
  48. * @since 1.1.11
  49. */
  50. public $controllerNamespace;
  51. /**
  52. * @var array mapping from controller ID to controller configurations.
  53. * Pleaser refer to {@link CWebApplication::controllerMap} for more details.
  54. */
  55. public $controllerMap=array();
  56. private $_controllerPath;
  57. private $_viewPath;
  58. private $_layoutPath;
  59. /**
  60. * Returns the name of this module.
  61. * The default implementation simply returns {@link id}.
  62. * You may override this method to customize the name of this module.
  63. * @return string the name of this module.
  64. */
  65. public function getName()
  66. {
  67. return basename($this->getId());
  68. }
  69. /**
  70. * Returns the description of this module.
  71. * The default implementation returns an empty string.
  72. * You may override this method to customize the description of this module.
  73. * @return string the description of this module.
  74. */
  75. public function getDescription()
  76. {
  77. return '';
  78. }
  79. /**
  80. * Returns the version of this module.
  81. * The default implementation returns '1.0'.
  82. * You may override this method to customize the version of this module.
  83. * @return string the version of this module.
  84. */
  85. public function getVersion()
  86. {
  87. return '1.0';
  88. }
  89. /**
  90. * @return string the directory that contains the controller classes. Defaults to 'moduleDir/controllers' where
  91. * moduleDir is the directory containing the module class.
  92. */
  93. public function getControllerPath()
  94. {
  95. if($this->_controllerPath!==null)
  96. return $this->_controllerPath;
  97. else
  98. return $this->_controllerPath=$this->getBasePath().DIRECTORY_SEPARATOR.'controllers';
  99. }
  100. /**
  101. * @param string $value the directory that contains the controller classes.
  102. * @throws CException if the directory is invalid
  103. */
  104. public function setControllerPath($value)
  105. {
  106. if(($this->_controllerPath=realpath($value))===false || !is_dir($this->_controllerPath))
  107. throw new CException(Yii::t('yii','The controller path "{path}" is not a valid directory.',
  108. array('{path}'=>$value)));
  109. }
  110. /**
  111. * @return string the root directory of view files. Defaults to 'moduleDir/views' where
  112. * moduleDir is the directory containing the module class.
  113. */
  114. public function getViewPath()
  115. {
  116. if($this->_viewPath!==null)
  117. return $this->_viewPath;
  118. else
  119. return $this->_viewPath=$this->getBasePath().DIRECTORY_SEPARATOR.'views';
  120. }
  121. /**
  122. * @param string $path the root directory of view files.
  123. * @throws CException if the directory does not exist.
  124. */
  125. public function setViewPath($path)
  126. {
  127. if(($this->_viewPath=realpath($path))===false || !is_dir($this->_viewPath))
  128. throw new CException(Yii::t('yii','The view path "{path}" is not a valid directory.',
  129. array('{path}'=>$path)));
  130. }
  131. /**
  132. * @return string the root directory of layout files. Defaults to 'moduleDir/views/layouts' where
  133. * moduleDir is the directory containing the module class.
  134. */
  135. public function getLayoutPath()
  136. {
  137. if($this->_layoutPath!==null)
  138. return $this->_layoutPath;
  139. else
  140. return $this->_layoutPath=$this->getViewPath().DIRECTORY_SEPARATOR.'layouts';
  141. }
  142. /**
  143. * @param string $path the root directory of layout files.
  144. * @throws CException if the directory does not exist.
  145. */
  146. public function setLayoutPath($path)
  147. {
  148. if(($this->_layoutPath=realpath($path))===false || !is_dir($this->_layoutPath))
  149. throw new CException(Yii::t('yii','The layout path "{path}" is not a valid directory.',
  150. array('{path}'=>$path)));
  151. }
  152. /**
  153. * The pre-filter for controller actions.
  154. * This method is invoked before the currently requested controller action and all its filters
  155. * are executed. You may override this method in the following way:
  156. * <pre>
  157. * if(parent::beforeControllerAction($controller,$action))
  158. * {
  159. * // your code
  160. * return true;
  161. * }
  162. * else
  163. * return false;
  164. * </pre>
  165. * @param CController $controller the controller
  166. * @param CAction $action the action
  167. * @return boolean whether the action should be executed.
  168. */
  169. public function beforeControllerAction($controller,$action)
  170. {
  171. if(($parent=$this->getParentModule())===null)
  172. $parent=Yii::app();
  173. return $parent->beforeControllerAction($controller,$action);
  174. }
  175. /**
  176. * The post-filter for controller actions.
  177. * This method is invoked after the currently requested controller action and all its filters
  178. * are executed. If you override this method, make sure you call the parent implementation at the end.
  179. * @param CController $controller the controller
  180. * @param CAction $action the action
  181. */
  182. public function afterControllerAction($controller,$action)
  183. {
  184. if(($parent=$this->getParentModule())===null)
  185. $parent=Yii::app();
  186. $parent->afterControllerAction($controller,$action);
  187. }
  188. }