Using MBMenu and Rights to automatically create menu item for your modules

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

Using Rights generator component, we can easily create a dynamic menu, in which we scan every modules' controllers, to access the index page of them.

Put the following code into your views/layouts/main.php file.

<div id="mainMbMenu">
		<?php
		$menuitems =  array(
				array('label'=>'Home', 'url'=>array('/site/index')),
				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
				array('label'=>'Contact', 'url'=>array('/site/contact')),
			);
		$rightGenerator = new RGenerator();
		$modules = array_keys(Yii::app()->getModules());
		foreach ($modules as $module) {
			$module_menu = array();
			$controllers = $rightGenerator->getControllersInPath(Yii::getPathOfAlias("application.modules.$module.controllers"));
			foreach ($controllers as $ctrl => $info) {
				$module_menu[] = array('label' => Yii::t($module, $info['name']), 'url' => array("/$module/" . lcfirst($info['name'])));
			}
			if (! empty($module_menu)){
				$menuitems[] = array('url' => array("#$module"), 'label' => Yii::t($module, $module),
							"items" => $module_menu);
			}
		}
		$this->widget('ext.widgets.mbmenu.MbMenu',array(
			'items'=> $menuitems,
		)); ?>
	</div><!-- mainmenu -->