Dynamic URL mapping

Hi,

From my understanding of MVC implementations, the default URL mapping is:


/controller/action/parameters

But with Yii, it’s also to do this:


/module/controller/action/parameters

However, neither gives me what I want as it’s being hard coded(?). I want to have dynamic URL mapping where:


/department/IT/forum/post/edit/5324

and


/department/Marketing/forum/post/edit/3256

will get the same module ‘forum’ which will handle all relevant requests within that module. The only difference is that the ‘forum’ module will enforce RBAC. However, after reading part of the documentation [1], I’m still unclear if this is possible in Yii. Would someone please point me in the right direction if this is achievable? I’d rather not roll my own framework ;)

Thanks.

[1] http://www.yiiframework.com/doc/guide/1.1/en/basics.module

This is easy!

If your module is ‘forum’, and the prefix is ‘department’, then all you need to do is pass a parameter (GET) around:


'/department/<identifier>/forum/post/<action:\w+>/<id:\d+>' => 'forum/<action>',

'/department/<identifier>/forum/posts' => 'forum/index'



I think that’s basically all you need.

Make sure that you pass the ‘identifier’ parameter in your routes:


'action' => createUrl(array('forum/view', 'identifier' => $forum_identifier)),

Written without testing - hope it helps.

Thanks for the fast reply. I apologize for not being clear enough in what I’m after. The sample /department/{name}/ is just a for example. The preceding part of the URL prior to ‘forum’ is completely dynamic and is definable by the user (of the application) such as


/region/Europe/deparment/customerService/forum/post/create

will still point to the same ‘forum’ module. I’d figure that the URL will be in the DB to ensure no conflicts and at which point it’s invoking the appropriate module. Note that the ‘region’ and ‘department’ could be its own module and/or controller, which is like nesting and/or interdependency of modules and/or controllers. The whole concept is being agile… I just make the modules. The user defines where and who has access to them for a given URL. :lol:

User configurable??

You can actually accomplish this if you limit your users somewhat.

Just use params.

It’s the bracketed elements in the url rule strings: <identifier>, etc.

If you follow my drift. :)

/region/<region_name>/department/<department_name>, and then module/controller/action (like normal url rules).

Yup, 99% user configurable! :D Anyway, I’ve found this by accident and was wondering if I can make it work for me by calling the appropriate module for a given URL via custom URL rule class [1]. What do you think? Thanks again.

[1]http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes

Yes, you found it!

I sort of forgot that.

Seems perfect for your case I think.