Menu problem (CMenu)

Hello everybody,

I’m new to Yii, and is just starting on the development of a small project.

I have encountered my first problem, that i was not able to solve by my self.

The system has a top menu and a menu in the side, and this sidemenu, has to work dynamicly, but i can’t get the active to work on the sidebar…

In my controller, the code looks like this:


	public $sidebarmenu	= array(

		array(

			'label'=>'Start',

			'url'=>array('index'),

			'active'=> (strcasecmp(Yii::app()->controller->route, 'site/index') === 0)  ? true : false,

			'items'=>array(

			array('label'=>'Create new post', 'url'=>array('create')),

			array('label'=>'Manage posts', 'url'=>array('manage')),

			),

		),

		array(

			'label'=>'Overview',

			'url'=>array('overview'),

			'active'=> (strcasecmp(Yii::app()->controller->route, 'site/overview') === 0)  ? true : false,

			'items'=>array(

			array('label'=>'Accounting', 'url'=>array('accounting')),

		),

		array(

			'label'=>'Users',

			'url'=>array('users')

		),

	);

Then in my sidebar view, it looks like this:


				if(isset($this->sidebarmenu))

				{

					$this->widget('zii.widgets.CMenu', array(

						'activeCssClass'=>'submenu-active',

						'activateParents'=>true,

						'activateItems'=>true,

						'htmlOptions'=>array(

							'class'=>'box'

						),

						'items'=>$this->sidebarmenu,

					));

				}

And the error i receave is:


Parse error: syntax error, unexpected '(' in C:\wamp\www\system\protected\controllers\SiteController.php on line 12

Call Stack

#	Time	Memory	Function	Location

1	0.0014	369512	{main}( )	..\index.php:0

2	0.0500	1917792	CApplication->run( )	..\index.php:13

3	0.0500	1917792	CWebApplication->processRequest( )	..\CApplication.php:158

4	0.0570	2133968	CWebApplication->runController( )	..\CWebApplication.php:136

5	0.0570	2133968	CWebApplication->createController( )	..\CWebApplication.php:271

I hope you understand my question/code :slight_smile:

Welcome to the forum.

You forgot to add something here




                        'items'=>array(

                        array('label'=>'Accounting', 'url'=>array('accounting')),



/Tommy

Where? :)




'items'=>array(

  array('label'=>'Accounting', 'url'=>array('accounting')),

),



/Tommy

It didn’t fix it :)

please paste your SiteController code @ pastie.org and tell us the link.

robert

Hello again and happy newyear :slight_smile:

My link is: http://pastie.org/3111343

Please take a look at http://www.php.net/m…properties.php. The initialization of class properties must be a constant value. You initialized the class property $sidebarmenu with some conditions.

You have to declare the property $sidebarmenu as blank array and to write a method like initSidebar. Into this you can put your declaration. Additional you have to overwrite the init method of the Controller class, to execute the initSidebar method.

example code:





class SiteController extends Controller

{

	/**

 	* Used for the sidebar

 	*/

	public $sidebarmenu = array ();


	public function init ()

	{

		$this->initSidebar ();

		

		parent::init ();

	}


	protected function initSidebar ()

	{

		$this->sidebarmenu = array (

			array (

				'label' => 'Start',

				'url' => array ('index'),

				'active' => (strcasecmp (Yii::app ()->controller->route, 'site/index') === 0) ? true : false,

				'items' => array (

					array ('label' => 'Create new post', 'url' => array ('create')),

					array ('label' => 'Manage posts', 'url' => array ('manage')),

				),

			),

			array (

				'label' => 'Overview',

				'url' => array ('overview'),

				'active' => (strcasecmp (Yii::app ()->controller->route, 'site/overview') === 0) ? true : false,

			),

			array (

				'label' => 'Users',

				'url' => array ('users')

			),

		);

	}... your code

}



Regards Robert

Thank you Robert! Supid error on my part. :slight_smile:

There is just one problem, cause when it’s done like this, the action of the controller is not yet called, and therefore you can’t make the link items active !? :slight_smile:

Yes you are right, my mistake ;).

Instead of


public function init ()

{

	$this->initSidebar ();


	parent::init ();

}

do this


public function beforeAction ()

{

	$this->initSidebar ();


	return true;

}

At this point (beforeAction) the request is fully routed to the action, that will be executed.

Regards Robert

Thnk you so much… That’s perfect ! :D