2014-04-09 41 views
0

我正在cakephp中建立一個應用程序,該應用程序具有管理員和用戶。 我爲兩者製作了不同的菜單。管理員可以查看所有菜單標題,但 用戶只有幾個。我已經寫代碼,但我不認爲view.Can你認爲它的任何小的方法,代碼優化刪除cakephp默認模板中的額外代碼

here is my code: 

    <?php 
     if ($this->Session->read('Auth.User')){ 

     if($this->Session->read('Auth.User.role')=='admin') 
     { 

     ?>  

管理部分

<div id='cssmenu'> 
<ul> 
    <li class='active'><?php echo $this->Html->link(
    'Home', 
    array(
    'controller' => 'users', 
    'action' => 'index', 
    'full_base' => true 
    ));?></li> 
    <li class='has-sub'><a href='#'><span>Manage Users</span></a> 
     <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
'Add New User', 
    array(
    'controller' => 'users', 
    'action' => 'add', 
    'full_base' => true 
    ));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
'InActive Users List', 
array(
    'controller' => 'users', 
    'action' => 'listUserInActive', 
    'full_base' => true 
));?> 

    </li> 
    <li class='has-sub'><?php echo $this->Html->link(
'Active Users', 
array(
    'controller' => 'manageUsers', 
    'action' => 'listUserActive', 
    'full_base' => true 
));?> 

    </li> 
    </ul> 
</li> 
<li class='has-sub'><a href='#'><span>Manage Consignment</span></a> 
    <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
'Add Consignment', 
array(
    'controller' => 'manageConsignments', 
    'action' => 'addConsignment', 
    'full_base' => true 
    ));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Consignment List', 
    array(
    'controller' => 'manageConsignments', 
    'action' => 'listConsignment', 
    'full_base' => true 
));?> 

     </li> 
    </ul> 
</li> 
<li class='has-sub'><a href='#'><span>Manage Parcels </span></a> 
    <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Add Parcel', 
    array(
    'controller' => 'manageParcels', 
    'action' => 'addParcel', 
    'full_base' => true 
));?> 
     </li> 
     <li class='has-sub'><?php echo $this->Html->link(
    'Parcel List', 
    array(
    'controller' => 'manageParcels', 
    'action' => 'listParcel', 
    'full_base' => true 
    ));?> 

    </li> 
    </ul> 
    </li> 
<li class='has-sub'><a href='#'><span>Manage Pallets </span></a> 
    <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Add Pallets', 
    array(
    'controller' => 'managePallets', 
    'action' => 'addPallet', 
    'full_base' => true 
    ));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Pallets List', 
    array(
    'controller' => 'managePallets', 
    'action' => 'listPallet', 
    'full_base' => true 
));?> 

    </li> 
    </ul> 
</li> 
<li class='has-sub'><a href='#'><span>Manage Profile </span></a> 
    <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
'Update Profile', 
array(
    'controller' => 'users', 
    'action' => 'editProfile', 
    'full_base' => true 
));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
'Update Password', 
array(
    'controller' => 'users', 
    'action' => 'updatePassword', 
    'full_base' => true 
));?> 

    </li> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Delete Account', 
    array(
    'controller' => 'users', 
    'action' => 'delete', 
    'full_base' => true 
    ), 
    array(), 
"Are you sure you wish to delete this Account?");?> 

    </li> 
    </ul> 
</li> 
<li><?php echo $this->Html->link(
'Logout', 
array(
    'controller' => 'users', 
    'action' => 'logout', 
    'full_base' => true 
));?> 

</ul> 
    </div> 


    <?php } 
這是最佳進場 默認

其他用戶部分

else {?> 

    <div id='cssmenu'> 
    <ul> 
    <li class='active'><?php echo $this->Html->link(
    'Home', 
    array(
    'controller' => 'users', 
    'action' => 'index', 
    'full_base' => true 
    ));?></li> 

    <li class='has-sub'><a href='#'><span>Manage Consignment</span></a> 
     <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Add Consignment', 
     array(
    'controller' => 'manageConsignments', 
    'action' => 'addConsignment', 
    'full_base' => true 
    ));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
'Consignment List', 
array(
    'controller' => 'manageConsignments', 
    'action' => 'listConsignment', 
    'full_base' => true 
));?> 

    </li> 
    </ul> 
</li> 
<li class='has-sub'><a href='#'><span>Manage Parcels </span></a> 
    <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
'Add Parcel', 
array(
    'controller' => 'manageParcels', 
    'action' => 'addParcel', 
     'full_base' => true 
    ));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Parcel List', 
     array(
    'controller' => 'manageParcels', 
    'action' => 'listParcel', 
    'full_base' => true 
    ));?> 

     </li> 
    </ul> 
</li> 
<li class='has-sub'><a href='#'><span>Manage Pallets </span></a> 
    <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
    'Add Pallets', 
    array(
    'controller' => 'managePallets', 
    'action' => 'addPallet', 
    'full_base' => true 
));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
'Pallets List', 
array(
    'controller' => 'managePallets', 
    'action' => 'listPallet', 
    'full_base' => true 
));?> 

    </li> 
    </ul> 
</li> 
<li class='has-sub'><a href='#'><span>Manage Profile </span></a> 
    <ul> 
    <li class='has-sub'><?php echo $this->Html->link(
'Update Profile', 
array(
    'controller' => 'users', 
    'action' => 'editProfile', 
    'full_base' => true 
));?> 
     </li> 
    <li class='has-sub'><?php echo $this->Html->link(
'Update Password', 
array(
    'controller' => 'users', 
    'action' => 'updatePassword', 
    'full_base' => true 
));?> 

    </li> 
<li class='has-sub'><?php echo $this->Html->link(
'Delete Account', 
array(
    'controller' => 'users', 
    'action' => 'delete', 
    'full_base' => true 
), 
    array(), 
"Are you sure you wish to delete this Account?");?> 

    </li> 
    </ul> 
</li> 
<li><?php echo $this->Html->link(
'Logout', 
array(
    'controller' => 'users', 
    'action' => 'logout', 
    'full_base' => true 
    ));?> 

     </ul> 
     </div> 
So can i create like oops concept so that which tabs are common 
to all that are added in else 

並在管理部分添加額外的選項卡。請提示我適當的方法。

+0

看看ACL模式以瞭解如何實現這一點。我建議你在db中製作菜單,並將每個項目授予用戶類型。當你製作你的樹結構時,請看這個http://stackoverflow.com/questions/22785181/build-a-tree-menu-with-li-tag-from-array讓你的樹形菜單 – Fury

回答

1

我只能建議把你的馬努到像數組:

$menu = array( 
    'item1' => array(
     'title' => 'Add Parcel', 
     'controller' => 'manageParcels', 
     'action' => 'addParcel', 
     'admin' => false, 
     // other fields used in menu 
    ), 
    'item1' => array(
     'title' => 'Add Consignment', 
     'controller' => 'manageConsignmens', 
     'action' => 'addConsignmen' 
     'admin' => true, 
     // other fields used in menu 
    ) 
) 

這樣你就可以通過數組循環,創造你的菜單dinamically

,你也可以爲您的陣列創建一個樹形結構考慮到子menues

進一步的步驟可以創建一個MenuHelper來管理所有

最後一步可以節省男人在你的數據庫中的一個表(即menu_items)中創建一個可以扮演一個樹的模型。最後把所有內容放入一個插件中。

我想已經存在的東西

+0

請你能提供一些例如或詳細說明。 – dipanshu

+2

你的問題太多了,所以答案會變得太長。嘗試着重於單點,我會盡力給你一些提示 – arilia

+0

感謝Arilia。如果你有它的例子,它使用jQuery和CSS,請轉發我[email protected] – dipanshu