CMenu, pages, clean URLs, and active items

Hi all,

I’ve scoured the Yii forums for an answer to this question:

How to get CMenu (and CNormalizeUrl in general) to turn


array('site/page','view'=>'name-of-my-page')

into a URL that looks like this:


http://mysite.com/site/page/view/name-of-my-page

and NOT like this:


http://mysite.com/site/page?view=name-of-my-page

I’ve tried declaring the CMenu URL like this:


array('site/page/view/name-of-my-page')

which works in terms of pulling up the page, but then CMenu does not recognize it as the active URL.

It’s a mystery to me why CNormalizeUrl sometimes adds the _GET parameters in clean URL format, and sometimes adds them in traditional _GET parameter format.

Can anyone help?

BTW, this thread looked like it was going to address the issue, but it didn’t:

http://www.yiiframew…+Url#entry96221

Thanks in advance.

For me it seems like it’s a url management problem.

Have you defined the good urls rules in your config file?

You can check how to define it here: http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Yes. I’ve modified the rules to accommodate this:




'rules'=>array(

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

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

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

   '~<view:\w+'=>'site/page',

);



This seems to work fine. But how do you get CNormalizeUrl to generate the link in that format? In my menu, I have:


array('label'=>'menuItemName', 'url'=>array('site/page','view'=>'pageName')),

But this generates [b]


/site/page?view=pageName

[/b]

instead of [b]


/site/page/view/pageName

[/b].

This isn’t the way it behaves if I use


array('label'=>'menuItemName', 'url'=>array('event/view','id'=>'event_id')),

which generates

[b]


/event/view/event_id

[/b]

Also, in a separate but related issue, the auto-generated active link styles are only appearing at the top level of my CMenu items.

If I input them like this:


array('label'=>'News', 'url'=>array('news/index')),

it highlights the link when I go to that page, but not when I go to the page’s children. I suspect that might be because of the /index in the routing.

But if I input it like this:


array('label'=>'News', 'url'=>array('news')),

it assumes ‘news’ should be tacked on to the current controller (which varies depending on where the menu is displayed).

And if I input it like this:


array('label'=>'News', 'url'=>array('/news')),

or this:


array('label'=>'News', 'url'=>'news'),

or this:


array('label'=>'News', 'url'=>'/news'),

it doesn’t highlight at all.

I have activateItems and activateParents set to true. I’m perplexed how one is supposed to make the parents recognize their children.

I think the 4th rule is somewhat broken.

And, even if it were written correctly, it won’t work for your purpose, because array(‘site/page’,‘view’=>‘pageName’)) will fall into the 3rd rule before it reaches 4th.

Would you please try the following?




'rules'=>array(

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

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

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

);

// or ...

'rules'=>array(

   'site/page/*'=>'site/page',

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

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

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

);