URLManager Rules

In my CMenu widget I have the following:


array('label'=>'Home', 'url'=>array('/site/index')),

array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),

array('label'=>'Events', 'url'=>array('/events/index')),

I currently use the following rules to take out the ‘site’ portion from the ‘Home’ link:


'rules'=>array(

	'index'=>'site/index',

),

I want these links to be displayed as follows:

Home - /webapp/

About - /webapp/about/

Events - webapp/events/

So basically I want to take out the ‘index’ portion from the ‘Home’ and ‘Events’ links and take out the ‘page’ and ‘view’ portions from the About link.

http://www.yiiframework.com/doc/guide/topics.url

Thanks jayrulez

I’ve already looked at that guide, can’t find anything that helps.

I could obviously hardcode the links for the ‘url’ attribute in CMenu but then I lose the active highlighting.

The problem is I have specified a urlSuffix, so the Events link for example currently appears as /webapp/events.html

It would be a lot better if it was /webapp/events/ and then sub-pages can be /webapp/events/id/100

So, anyone able to help?

You should first consider every different URL you will need and than decide for he URL format…

for your example /webapp/events/id/100 => is that events/update or events/view…


For the first post:




        'urlManager'=>array(

            'urlFormat'=>'path',

            'showScriptName'=>false,

            'urlSuffix'=>'',

            'rules'=>array(

                '/'=>'site/index',

                '<view:(about)>'=>'site/page',

                '<_c:(Events)>'=>'<_c>\index',

            )

        ),



so urlFormat is path and .htaccess should be set too (you already should have done that)

urlSuffix - if you want a suffix like “.html” write here… if you don’t want it, delete this line

‘/’ => ‘site/index’ - translates root of webapp to site/index

‘<view:(about)>’=>‘site/page’ - translates to site/page?view=about, if you have more static pages just add them in the parentesis, eg. if you have static pages info and sitemap you would put : ‘<view:(about|info|sitemap)>’=>‘site/page’

‘<_c:(Events)>’=>’<_c>\index’ (not tested) - translates Events to events/index, same as above if you have more models like User or Post you can add them here like this

‘<_c:(Events|User|Model)>’=>’<_c>\index’

Thanks mdomba. The first two solutions you posted above work perfectly - thanks!

The last solution does not seem to work. Basically what I have is the following:

Menu:


array('label'=>'Events', 'url'=>array('/events/index')),

Config:




'urlManager'=>array(

        'urlFormat'=>'path',

	'showScriptName'=>false,

	'rules'=>array(

		'events' => 'events/index',

	),

	'urlSuffix'=>'.html',

),



What I want to acheive:

Main Menu Events page link: /webapp/events/ (this is currently displaying as /webapp/events.html)

Events page sub links: /webapp/events/view/id/1.html (this is currently displaying OK)

Anyone able to advise regarding my above query? Thanks! B)

You can set urlSuffix for a particular rule like that:




'blablabla' => array(..., 'urlSuffix'=>'.html')



or don’t set it…

Fantastic! Man I love Yii, it is so flexible! ;D

I was struggling to get the about page to work. I’ve fixed it, but thought I would post the reason.

My rules read:




'rules'=>array(

	'/' => 'site/index',

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

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

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

	'<view:(about)>' => 'site/page',

),



but the view needs to be before the controller section. I presume the controller is overriding it.




'rules'=>array(

	'/' => 'site/index',

	'<view:(about)>' => 'site/page',

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

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

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

),



Yii applies the rules in order, so if you apply the controller rule first, the view rule no longer matches.