CMenu Url Matching isItemActive

Hi all!

I’m not sure it this is correct or the best way of doing it, but it works (I think). So I thought it might be useful to someone.

I’ve applied some routes with the urlManager to the menu, instead of urls like “site/services” I made in config

'urlManager' => array(

			'showScriptName' => false, 

			'urlFormat' => 'path',




in the main layout with CMenu widget, if you see the items don’t have “controller/action”




							array('label'=>'SERVICES ', 'url'=>array('/services')),


					)); ?>

The CMenu isItemActive() method is not able to match because


will never be true because

trim($item['url'][0],'/')= "services"

$route = "site/services" 

So I extended the CMenu isItemActive() to

protected function isItemActive($item, $route) {

		if (isset($item['url']) && is_array($item['url'])) {

			if (Yii::app()->getRequest()->url == $item['url'][0]) {

				return true;

			} elseif (!strcasecmp(trim($item['url'][0], '/'), $route)) {

				return false;

			} elseif (count($item['url']) > 1) {

				foreach ( array_splice($item['url'], 1) as $name => $value ) {

					if (!isset($_GET[$name]) || $_GET[$name] != $value)

						return false;



				return true;



			return false;



And like that works.

I’m no expert in Yii or PHP, just a curious, so if anything is wrong please say it.

Thank you all for the help in other issues.

I’m loving it…

I guess what you’ve done works, but consider this:

The developer has knowledge of the routes and URL parameters to use. Therefore for internal references use urls with a route parameter.

The UrlManager is meant to map urls from the outside world to urls used internally by yii.

Thank you Onman for your comment, but I don’t quite understand.

If we know all the routes why change in

And if I map something like to,why if someone goes directly to the site with and then clicks on the menu item of services should see in the browser instead of ?

Sorry for being such a newbie, but I just want to learn.


Internally Yii uses urls like ‘’.

To have users enter easier readable names you can use CUrlManager to display nicer urls like’ (this could be a replacement for the previous internal example, you can generate almost any form you’d like).

In your programs you should always use the internal format. When clicked yii automatically presents the user with the user friendly version.

read more about this: