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',
'<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
Yii - Controllers. Controllers are responsible for processing requests and generating responses. After user's request, the controller will analyze request data, pass them to model, then insert the model result into a view, and generate a response.
config This folder holds the configuration files that include things like connecting Yii with your database, setting up e-mail sending, reformatting URLs and setting globally available parameter values. controllers 'C' of MVC. Controllers are the heart of Yii.
The backend directory is a complete Yii structure, similar to what you will find in the Basic Template. You will find the following subdirectories: assets config controllers models runtime
For example, in the Yii releases, there are yii\web\ViewAction and yii\web\ErrorAction, both of which are standalone actions. To use a standalone action, you should declare it in the action map by overriding the yii\base\Controller::actions () method in your controller classes like the following:
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>', ) ),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With