Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii - Controller in subdirectory

Tags:

yii

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

like image 547
user2251 Avatar asked Jul 10 '11 11:07

user2251


People also ask

What is Yii controller in Yii?

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.

What are the Yii configuration files?

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.

What is the backend directory in 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

How do I use a standalone action in Yii?

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:


3 Answers

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.

like image 91
hdang Avatar answered Sep 21 '22 15:09

hdang


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]

like image 33
ldg Avatar answered Sep 22 '22 15:09

ldg


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>',
            )
        ),
like image 45
wanshanju Avatar answered Sep 21 '22 15:09

wanshanju