Yii – Example of Creating a submenu widget

  • Yii Articles

  • Blog Subjects

  • 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 |  copy code |? 
    01
    02
    <?php
    03
    // if controller name matches current controller then this must be the active menu
    04
    function activeMenu($cid, $menuName) {
    05
        $activeMenu=($cid==$menuName) ? array('active') : '';
    06
        return $activeMenu;
    07
    }
    08
    ?>
    09
    <div class="container" id="page">
    10
     
    11
    	<div id="mainmenu">
    12
    		<?php  
    13
     
    14
                    $cid=Yii::app()->controller->id;
    15
                    if (Yii::app()->user->isGuest) {
    16
                        $menu=array (
    17
                            	array('label'=>Yii::t('user','Login'), 'url'=>array('/site/login'), ),
    18
    				array('label'=>Yii::t('user','Register'), 'url'=>Yii::app()->getModule('user')->registrationUrl,  ),
    19
    		    );
    20
                    } else {
    21
                        $user=Yii::app()->getModule('user')->user();
    22
                        //var_dump($user);
    23
                        if ($user->superuser) {
    24
                            $menu=array (
    25
                                    array('label'=>Yii::t('user','Manage User'),'url'=>'/user/admin'),
    26
                                    array('label'=>Yii::t('user','List User'),'url'=>'/user')
    27
                                );
    28
                        } else {
    29
                            $menu=array (
    30
    				array('label'=>Yii::t('user','Subscription Details'), 'url'=>array('/user/profile'),'active'=>activeMenu($cid,'profile')), 
    31
    				/** Other User menus **/
    32
    				array('label'=>Yii::t('user','Logout').' ('.$user->email.')','url'=>Yii::app()->getModule('user')->logoutUrl,'itemOptions'=>array('class'=>'right') ),
    33
                                );
    34
                        }
    35
     
    36
                    }
    37
     
    38
                    $this->widget('zii.widgets.CMenu',array(
    39
                                        'items'=>$menu,
    40
                                        'activateItems'=>true,
    41
                                    ));
    42
                    ?>
    43
    	</div><!-- mainmenu -->
    44
     
    45
            <div id="submenu">
    46
                <?php $this->widget('SubMenu'); ?>
    47
            </div>
    48