Support for parent-child (drop down) menu structure


It would be nice if pages generated while creating blank YII application supports a parent-child (drop down) menu structure.

Default menu can be kept as it is. If users want a parent-child menu structure, it is better if users can extend it.

For example menu items declared in protected/views/layouts/main.php can be created like this.

<div id="mainmenu"  align="center">

<?php $this->widget('application.components.MainMenu',array(


		array('label'=>'Parent menu', 'url'=>array('/site/index'),'id'=>'idParent'),

		array('label'=>'Child menu', 'url'=>array('/site/contact'), 'parent'=>'idParent','id'=>'idChild'),

		array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest,'id'=>'idLogin'),

		array('label'=>'Logout', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest,'id'=>'idLogout')


)); ?>

</div><!-- mainmenu -->

Here two new elements are added to menu item array called parent and id.

Id is a unique identifier for menu item and parent contains the id of parent menu item.

We can also introduce another parameter to array called ‘menuView’=>‘dropDownMenu’. This will allow user to set a different view other than mainMenu view used in protected/components/MainMenu.php.

Finally we can modify protected/components/MainMenu.php as this.

        public $items=array();

        public $menuView = 'mainMenu';//New variable to hold menu view

	public function run()





		foreach($this->items as $item)


			if(isset($item['visible']) && !$item['visible'])





                        //Sets id and parent


                        if (isset($item['parent']))












                //Set view set by user





We do not plan on modifying the menu widget that yiic generates for you. However, a separate menu widget is currently being developed for the Zii extension library which has many more features, as well as nested items.