Yii Framework Forum: CMenu Url Matching isItemActive - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CMenu Url Matching isItemActive Extending CMenu Rate Topic: -----

#1 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 02-January 10

Posted 16 February 2010 - 04:49 AM

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...
0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 16 February 2010 - 11:51 AM

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.
0

#3 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 02-January 10

Posted 16 February 2010 - 01:02 PM

View PostOnman, on 16 February 2010 - 11:51 AM, said:

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
0

#4 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 16 February 2010 - 01:33 PM

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.yiiframew...uide/topics.url
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users