zend框架-在组合zend_Acl和zend_Navigation时遇到问题



我有一个Federico_Plugin_Acl,它扩展了Zend_Controller_Plugin_Abstract,看起来像这样:

class Federico_Plugin_Acl extends Zend_Controller_Plugin_Abstract {
private $_acl = null;
private $_auth = null;
const DEFAULT_ROLE = 'guest';
public function __construct($auth) {
    $this->_auth = $auth;
    $this->_acl = new Zend_Acl();
    $this->_acl->addRole(new Zend_Acl_Role(self::DEFAULT_ROLE));
    $this->_acl->addRole(new Zend_Acl_Role('user'), self::DEFAULT_ROLE);
    $this->_acl->addRole(new Zend_Acl_Role('admin'), 'user');
    $this->_acl->addResource(new Zend_Acl_Resource('index'));
    $this->_acl->addResource(new Zend_Acl_Resource('users'));
    $this->_acl->addResource(new Zend_Acl_Resource('about'));
    $this->_acl->addResource(new Zend_Acl_Resource('gisele'));
    $this->_acl->addResource(new Zend_Acl_Resource('admin'));
    $this->_acl->allow('guest', 'index');
    $this->_acl->allow('guest', 'about');
    $this->_acl->deny('guest', 'gisele');
    $this->_acl->deny('guest', 'users');
    $this->_acl->allow('user', 'users', array('index')); 
    $this->_acl->allow('admin', 'users');
    $this->_acl->allow('admin', 'gisele');
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
    if ($this->_auth->hasIdentity()) {
        $role = $this->_auth->getStorage()->read()->role;
    } else {
        $role = self::DEFAULT_ROLE;
    }
    $action = $request->getActionName();
    $controller = $request->getControllerName();
    if ($this->_acl->has($controller)) {
        if (!$this->_acl->isAllowed($role, $controller, $action)) {
            $request->setActionName('login');
            $request->setControllerName('index');
        }
    }
}
}

这个方法在我的引导程序中使用了这个类:

protected function _initNavigation()
{
    $this->_auth = Zend_Auth::getInstance();
    $this->_acl = new Federico_Plugin_Acl($this->_auth);
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
    $navigation = new Zend_Navigation($config);
    $roleAuth = $this->_auth->getIdentity();
     if(null == $roleAuth)
        $role = 'guest';
    else
        $role = $roleAuth->role;
    $view->navigation($navigation)->setAcl($this->_acl)->setRole($role);
}

通过这样设置这些配置,我得到以下错误:

可捕获的致命错误:传递给Zend_View_Helper_Navigation_HelperAbstract::setAcl()必须是Zend_Acl的实例,给定Federico_Plugin_Acl的实例,在第118行的/home/fiodorovic/public_html/gisele/application/Bootstrap.php中调用,并在第333行的/hom/fiodorovic/library/ZendFramework/library-Zend/View/Helper/Navigation/HelperAbtract.php中定义调用堆栈

这是意料之中的,因为Federico_Plugin_Acl是Zend_Controller_Plugin_Abstract的一个实例。。。尽管如此,如果我扩展Zend_Acl,我会得到这个错误:

Fatal error: Zend_Acl_Role_Registry_Exception: Role 'guest' not found in /home/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php on line 522

所以。。。我已经试着解决这个问题一段时间了,。。。但似乎没有让它正常工作。。。你知道我在这里遗漏了什么吗?

Zend_Controller_Plugin_Abstract和Zend_Acl完全不同。你想做的是从你的插件中取出ACL对象(现在在私有部分),并将其传递给
$view->navigation($navigation)->setAcl(<here>);

相关内容

  • 没有找到相关文章

最新更新