Yii - Example of Creating a submenu widget

This is a continuation of two previous articles, How to make a dynamic Sub-menu and How to make a dynamic sub-menu – UPDATE, which show the mechanisms by which you can build a dynamic submenu system, populating the dropdowns from the relevant controller.  For example; with a customer booking system, if you had a menu for customers and a menu for bookings, you may wish to control the customer drop down from the customer controller and the bookings dropdown from the bookings controller.

This post will bring these two previous posts into reality with an example layout view.

The first factor is that your main menu line will remain as you have already defined it, probably using a cMenu class.

Your submenu is defined as a DIV element immediately after your main menu container.

You can then manage the visual integration of these two elements using CSS.


  <?php
  // if controller name matches current controller then this must be the active menu
  function activeMenu($cid, $menuName) {
    $activeMenu=($cid==$menuName) ? array('active') : '';
    return $activeMenu;
  }
  ?-->


  <div id="mainmenu">
    <?php
    $cid=Yii::app()->controller->id;
    if (Yii::app()->user->isGuest) {
      $menu=array (
          array('label'=>Yii::t('user','Login'), 'url'=>array('/site/login'), ),
          array('label'=>Yii::t('user','Register'), 'url'=>Yii::app()->getModule('user')->registrationUrl,  ),
          );
                } else {
                    $user=Yii::app()->getModule('user')->user();
                    //var_dump($user);
                    if ($user->superuser) {
                        $menu=array (
                                array('label'=>Yii::t('user','Manage User'),'url'=>'/user/admin'),
                                array('label'=>Yii::t('user','List User'),'url'=>'/user')
                            );
                    } else {
                        $menu=array (
        array('label'=>Yii::t('user','Subscription Details'), 'url'=>array('/user/profile'),'active'=>activeMenu($cid,'profile')), 
        /** Other User menus **/
        array('label'=>Yii::t('user','Logout').' ('.$user->email.')','url'=>Yii::app()->getModule('user')->logoutUrl,'itemOptions'=>array('class'=>'right') ),
                            );
                    }
                }
                $this->widget('zii.widgets.CMenu',array(
                                    'items'=>$menu,
                                    'activateItems'=>true,
                                ));
                ?>
  </div><!-- mainmenu -->
        <div id="submenu">
            <?php $this--->widget('SubMenu'); ?>
        </div>
</div>


Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...


Beginning Yii ...

I'm very excited about a new Yii Training course that has just been published.

The course is designed for anyone wanting to learn Yii, whether new to frameworks or converting from another.

Eight chapters, each having five videos of between 3 and 5 minutes, take you through the basics of the Yii Framework, building a web application step-by-step.

Save 40% - introductory offer $20.99 / £13.19 / €16.19 / A$20.99

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com