我正在cakephp中构建一个应用程序,其中包含管理员和用户。 我为两者制作了不同的菜单。 admin可以查看所有菜单标题但是 用户很少。我已经编写了代码,但我认为这不是优化方法 对于默认视图。您可以建议任何小方法,代码优化
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 :(得分:1)
我只能建议将你的manu放入如下数组:
$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
)
)
所以你可以循环播放数组并以动态方式创建菜单
您还可以为阵列创建树结构,以考虑子菜单
进一步的步骤可能是创建一个MenuHelper来管理所有
最后一步可以将菜单保存在数据库中的表(即menu_items)中,创建一个作为Tree的模型。最后将所有内容放入插件中。
我猜某事已经存在