2

I was wondering what's the best way to have a nested URL structure in Yii I have a website with a structure like the following

/index
/dashboard (dashboard controller, index action)
/dashboard/profile (profile controller, index action)
/dashboard/profile/view (profile controller, view action)
/dashboard/profile/update (profile controller, update action)
/dashboard/stats (stats controller, index action)

and so on...

Basically /dashboard has it's own controller and by default will use the Index action however I would like to nest other controllers under /dashboard, e.g. /dashboard/profile however, the /profile controller is not an action, it should be a controller which in turn can have it's own actions. e.g. /dashboard/profile/view

is this doable? if yes, what would be the best way to achieve such a structure?

5
  • 2
    You are probably looking to use a module. Commented Jun 3, 2013 at 11:27
  • In MVC that the URL defines the structure i.e. controller/action/parameter is the basic structure of MVC. ext element after 1st slash will be taken as function name Commented Jun 3, 2013 at 11:28
  • @Ahmed isn't it possible to have the URL structure re-disegned so that /dashboard/profile will be translated to /profile (controller) ? Commented Jun 3, 2013 at 12:23
  • I t is. But then you will have to move into the directories as we do for admin side in our websites. Commented Jun 3, 2013 at 12:27
  • @Ahmed what you are describing seems a good solution for what I am trying to achieve. Extra directory levels would actually be helpful for me. Any tips regarding the url rewriting rules? Commented Jun 3, 2013 at 13:37

2 Answers 2

3

If you have followed the tutorial on the Yii site to hide index.php from your application:

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false,
    'caseSensitive' => false,
    'rules' => array(

        'dashboard/<controller:\w+>/<action:\w+>' => '/dashboard/<controller>/<action>',
        'dashboard/<controller:\w+>' => '/dashboard/<controller>/index',

        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        '<controller:\w+>/' => '<controller>/index',

        ...
Sign up to request clarification or add additional context in comments.

3 Comments

that seems all good and thank you for that, however the dashboard controller is still called. I get a 404 page unless I specify an action within the dashboard controller DashboardController.php E.g. actionProfile(). Whereas I would like the profile controller ProfileController.php to be called
It's weird, I have managed to make it work by setting 'caseSensitive' to true. Whenever it's set to false I get a 404 with Unable to resolve the request "Profile/index"
ok @Samuel Liew solved, I never realized before that controller files should only have the first letter uppercase e.g. ProfileviewController and not ProfileViewController Also, managed to simply the first two rules to 'dashboard/<controller:\w+>/<action:\w+>' => '<controller>/<action>', AND 'dashboard/<controller:\w+>' => '<controller>/index',
2

There is one more way that probably wasn't mentioned. Yesterday I would answer "use modules" but today I learned (and tested) that it is enough to add sub-folders to folder "controllers" and "views". For example folder: "/controllers/dashboard" with file "ProfileController.php" will enable URL: "myproject.com/dashboard/profile/action" and views will be searched in folder "views/dashboard".

But there is the problem, that you cannot use URL: "myproject.com/dashboard/" as "dashboard" is only a folder name. So you will probably have to use modules.

YouTube::Kurt Clement - Yii - UrlManager

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.