Yii framework supports modules and also subdirectories in controllers directory, so path to some specific action could be
/index.php?r=module/controller/action
or/index.php?r=subdirectoryInControllerDir/controller/action
.
My goal here is to have multiple subdirectories in the controllers directory. Inside these folders I would create controllers with the same names as parent ones using namespaces.
However if I write
namespace mynamespace;
class MyController extends \MyController {
}
Yii would load MyController
instead of mynamespace\MyController;
Any suggestions here?
Yii uses an intuitive naming convention for namespaces, which starts from \application and must follow the physical directory structure, like the built-in autoload config. If your base MyController class is in protected/controllers/, then it should use
namespace application\controllers;
and the child MyController in protected/controllers/subdir/
To make a request like "subdir/my" work, you need to add the following code to
CWebApplication::createController()
(or inherit it in a subclass) right after the class file is included:If you have set
controllerNameSpace
ofCWebApplication
you can also use that value instead of hardcoding\\application\\controllers\\
.