0
votes

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?

2
You are probably looking to use a module. - Jon
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 - Ahmed
@Ahmed isn't it possible to have the URL structure re-disegned so that /dashboard/profile will be translated to /profile (controller) ? - Martin
I t is. But then you will have to move into the directories as we do for admin side in our websites. - Ahmed
@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? - Martin

2 Answers

2
votes

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',

        ...
1
votes

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