【问题标题】:How can I get $acl->isAllowed in a action helper?如何在动作助手中获得 $acl->isAllowed?
【发布时间】:2014-01-06 08:22:54
【问题描述】:

我正在尝试创建一个用于创建菜单的操作助手。 但是我不知道为什么它不知道函数isAllowed。我收到以下错误消息:

在非对象上调用成员函数 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);
    }
}

【问题讨论】:

  • 如果设置了 $this->_acl is 那么你需要使用它,而不是 $acl。 (不要使用 Zend,所以我可能是错的)。
  • 我仍然收到相同的错误消息,我更新了它现在工作的问题我需要实例化 Zend_Acl 也谢谢
  • @웃웃웃웃웃 停止你的编辑狂潮,我已将你标记为版主注意。

标签: php function zend-framework frameworks helper


【解决方案1】:

我不是 Zend 专家,但是:

  • $this-&gt;_acl = new Zend_Acl(); - 这将创建没有任何角色集的新 ACL 对象,所以它是无用的(也许你想这样做 - 首先获取插件,如果没有创建新的?)
  • -&gt;getPlugin('Acl'); - 你应该使用完整的插件类名

我通常在插件中创建和启动 ACL 对象(在 preDispatch 上)并将其保存在 Zend_Registry 中,以便我以后可以使用 Zend_Registry::get('Zend_Acl')-&gt;isAllowed($role, $resource, $privilege);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多