How To Identify Currently Active Menu Item

In my top nav, I want a class of “active” on the menu item if the user is currently viewing that page. The only way I’ve figured out how to do this so far is to fetch the route from the controller and manually compare to a known value. This seems like a very poor solution. Can someone help me do this the correct way?




public function properties()

	{

		$route = Yii::app()->getController()->getRoute();

		return array(

			'items'=>array(

				//this item works, but is there a better way?

				array('label'=>$this->t('Home'), 'url'=>aUrl('/'), 'active' => $route === 'base/default/index' ),

				array('label'=>$this->t('How {site} Works', array('{site}'=>app()->name)), 'url'=>url('base/page', array('view' => 'how-it-works'))),

			),

		);

	}







This functionality should be done by default with the CMenu Widget.

Here is the code from CMenu that handles it:


        /**

         * Checks whether a menu item is active.

         * This is done by checking if the currently requested URL is generated by the 'url' option

         * of the menu item. Note that the GET parameters not specified in the 'url' option will be ignored.

         * @param array $item the menu item to be checked

         * @param string $route the route of the current request

         * @return boolean whether the menu item is active

         */

        protected function isItemActive($item,$route)

        {

                if(isset($item['url']) && is_array($item['url']) && !strcasecmp(trim($item['url'][0],'/'),$route))

                {

                        if(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;

        }