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.

  // 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">
    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 {
                    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') ),
  </div><!-- mainmenu -->
        <div id="submenu">
            <!--?php $this--->widget('SubMenu'); ?>

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 ...

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com