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

			'rules'=>array(

				'services'=>'site/services')

			)),



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


$this->widget('application.components.XMenu',array(

						'id'=>'mainmenu',

						'items'=>array(

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

						),

					)); ?>

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


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

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 www.mysite.com/posts/view/id/1 in www.mysite.com/post/1?

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

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

Thanks

Internally Yii uses urls like ‘http://example.com/index.php?r=/module/controller/action&param=value&param2=value2’.

To have users enter easier readable names you can use CUrlManager to display nicer urls like

http://example.com/action/value’ (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: http://www.yiiframework.com/doc/guide/topics.url