From 21160338d7a2dbad04228028b00172ae3fb6602a Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 2 Jun 2013 22:31:17 -0400 Subject: [PATCH] Implemented default controller namespace. --- apps/basic/config/main.php | 1 - framework/yii/base/Module.php | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/apps/basic/config/main.php b/apps/basic/config/main.php index 9adfba6..054259e 100644 --- a/apps/basic/config/main.php +++ b/apps/basic/config/main.php @@ -4,7 +4,6 @@ return array( 'id' => 'bootstrap', 'basePath' => dirname(__DIR__), 'preload' => array('log'), - 'controllerNamespace' => 'app\controllers', 'modules' => array( // 'debug' => array( // 'class' => 'yii\debug\Module', diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index 060d321..fac4164 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -88,7 +88,11 @@ abstract class Module extends Component */ public $controllerMap = array(); /** - * @var string the namespace that controller classes are in. Default is to use global namespace. + * @var string the namespace that controller classes are in. If not set, + * it will use the "controllers" sub-namespace under the namespace of this module. + * For example, if the namespace of this module is "foo\bar", then the default + * controller namespace would be "foo\bar\controllers". + * If the module is an application, it will default to "app\controllers". */ public $controllerNamespace; /** @@ -178,6 +182,16 @@ abstract class Module extends Component public function init() { $this->preloadComponents(); + if ($this->controllerNamespace === null) { + if ($this instanceof Application) { + $this->controllerNamespace = 'app\\controllers'; + } else { + $class = get_class($this); + if (($pos = strrpos($class, '\\')) !== false) { + $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; + } + } + } } /**