Yii - Controller in subdirectory
I'm facing an issue with Yii Framework routing.
I've created controller, let's call it TestController.php
Then, I need to put it into a subdirectory called Make, so my structure would look like:
controllers/TestController.php
controllers/Make/TestController.php
Of ocurse, if I change it's name, it works perfectly but is there any way to put a controller of the same name in controllers directory and a subdirectory?
Edit
My URLManager config looks like:'urlManager'=>array(
'showScriptName' => false,
'urlFormat'=>'path',
'rules'=>array(
'gii' => 'gii',
'gii/<controller:\w+>' => 'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>' => 'gii/<controller>/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',开发者_Go百科
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
I have a controller Bookmarks
. As I have some other things related to the bookmarks, I needed to create a directory bookmarks
and put some controllers there, for example Categories
.
Can't force to make it work.
Edit 2
Just checked clean application. It seems to be a Yii bug (?).Edit 3
I've changed import configuration, as suggested:'import'=>array(
'application.models.*',
'application.components.*',
'application.controllers.bookmarks.*'
),
I have also created a correct route rule 'bookmarks/<controller:\w+>/<action:\w+>'=>'bookmarks/<controller>/<action>',
.
My files structure is now as following:
BookmarksController.php
bookmarks/CategoriesController.php
Here's an exceptions that's being thrown:
exception 'CHttpException' with message 'The system is unable to find the requested action "categories".' in /home/root/www/yiitesting/framework/web/CController.php:477
Before making any subdirectory, be aware that Yii autoload function doesn't search subdirectories: Yii want to autoload the TestController class in the case of Controller, so add application.controllers.Make.* in your import declaration:
'import'=>array(
.....
'application.controllers.Make.*',
),
and of course you must add a rule to urlManager to help Yii look up correct Controller like @ldg did.
notes: in this case, Yii will look for views/Make/* for the view.
You should be able to update your URL Manager with an entry like:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'Make/<controller:\w+>/<action:\w+>'=>'Make/<controller>/<action>',
...
then access that controller via /Make/test[/action]
My nginx config:
rewrite ^/(.*) /index.php last;
My Yii urlManager config:
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName' => false, 'rules'=>array( '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', '<path:\w+>/<controller:\w+>/<action:\w+>'=>'<path>/<controller>/<action>', ) ),
The following urlManager config also works:
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName' => false, 'rules'=>array( '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', '<abc:\w+>/<controller:\w+>/<action:\w+>'=>'<abc>/<controller>/<action>', ) ),
Did you try accessing through /Make/test instead of /test? This feature should work out of the box. Cheers
精彩评论