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


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">
        $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);
            'items'=> $menuitems,
        )); ?>
    </div><!-- mainmenu -->

Total 2 comments

#6942 report it
AlDente at 2012/02/15 06:55am
Error - Answer

I too have encountered the same error The function "getControllersInPath" exist in the class RGenerator, but it's protected function. I have rewrite the declaration of function without . The code for mbMenu works. There is problem in this my operation ? Thanks

#4223 report it
at 2011/06/16 10:47pm

I was to try this but i have error :

RGenerator and its behaviors do not have a method or closure named "getControllersInPath".

Leave a comment

Please to leave your comment.

Write new article