簡體   English   中英

如何獲得動作助手中的$ acl-> isAllowed?

[英]How can I get $acl->isAllowed in a action helper?

我正在嘗試創建用於創建菜單的動作幫助器。 但是我不知道為什么它不知道允許該功能。 我收到以下錯誤消息:

在非對象上調用成員函數isAllowed()

<?php
class Zend_Controller_Action_Helper_Menu extends Zend_Controller_Action_Helper_Abstract
{
    private $_acl;
    public function createSubMenu($request,$identity){
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        $identity = $identity->user_role;
        $access = $this->hasAccess($identity,$action,$controller);

        $return;

        $return .= "<ul>";
            $return .= "<li><a href=''>".$identity."</a></li>";
        $return .= "</ul>";

        return $access;
    }

    private function hasAccess($role, $action, $controller)
    {
        $this->_acl = new Zend_Acl();
        if (!$this->_acl) {
            $this->_acl = Zend_Controller_Front::getInstance()->getPlugin('Acl');
        }
        return $this->_acl->isAllowed($role, $controller, $action);
    }
}

我不是Zend專家,但是:

  • $this->_acl = new Zend_Acl(); -這將創建沒有任何角色集的新ACL對象,因此它是無用的(也許您想以其他方式進行此操作-首先獲取插件,如果沒有一個可以創建新對象?)
  • ->getPlugin('Acl'); -您應該使用完整的插件類名稱

我通常在插件中(在preDispatch )創建並初始化ACL對象,並將其保存在Zend_Registry中,以便以后可以使用Zend_Registry::get('Zend_Acl')->isAllowed($role, $resource, $privilege);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM