2012-05-27 76 views
0

按鈕和錨我使用ACL授予資源角色在系統中,允許的動作是excuted並否認行動被路由到自定義頁面,我要顯示和隱藏菜單元素在運行時使用ACL上的資源,並且我想要顯示和隱藏錨點,視圖中的按鈕。隱藏和顯示導航菜單項,使用ACL

我做了一個輔助類

class Zend_View_Helper_Permission extends Zend_View_Helper_Abstract 
    { 
    private $_acl; 
    public function hasAccess($role, $action, $controller) 
    { 
     if (!$this->_acl) { 

      $this->_acl = Zend_Registry::get("Acl"); 
    } 

    return $this->_acl->isAllowed($role, $controller, $action); 
    } 
} 

我定義視圖助手在config.ini文件這樣

resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/modules/privileges/views/helpers" 

我如何使用這個幫手,使在運行時創建的看法?

回答

1

你的方法名稱應與類名,因此它應該是允許的,而不是hasAccess。

我自己用的一個全球性的方法秀()而不是使用視圖助手

function show($action = null) 
    { 

     $request = Zend_Controller_Front::getInstance()->getRequest(); 
     $action = $action === null ? $request->getActionName() : $action; 
     $module = $request->getModuleName(); 
     $controller = $request->getControllerName(); 

     if(!Zend_Registry::isRegistered('acl')) throw new Exception('Show function can only be called inside view after preDispatch'); 

     $acl = Zend_Registry::get('acl'); 
$resource = $module . '#' . $controller; 
     return $acl->isAllowed(Zend_Auth::getInstance()->getIdentity(),$resource,$action); 
    } 

爲了保持它的簡單需要控制器,從請求對象模塊名稱。 要隱藏列表動作視圖編輯操作鏈接只是鬥

list.phtml代碼如下

<h2>Listing page Only superadmin can see edit link</h2> 
<?php if(show('edit')): ?> 
<a href="<?echo $this->url(array('action'=>'edit')) ?>">Edit</a> 
<?php endif;?> 

更新

全局函數顯示了其內部裝載庫/ Util.php中定義 public/index.php

require_once 'Zend/Application.php'; 
require_once 'Util.php'; 
+0

你在哪裏定義了這個全局變量?你是否在插件中定義了它? – palAlaa

+0

我定義了它更容易使用,但你也可以使用視圖助手。我已經更新了我的答案,並在信息中定義了它。 –

+0

我怎樣才能使它的航海家,因爲我創建使用XML文件呢?? – palAlaa