2

我有 application\plugins\Acl.php 与:

class Application_Plugin_Acl extends Zend_Controller_Plugin_Abstract {
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
    $acl = new Zend_Acl();

我在那里添加角色。我也有助手 HasAccess.php:

class Zend_View_Helper_HasAccess extends Zend_View_Helper_Abstract {
    public function hasAccess($role, $action, $controller) {
    if (!$this->_acl) {
        $acl = Zend_Controller_Front::getInstance()->getPlugin('Application_Plugin_Acl');
    }
    return $acl->isAllowed($role, $controller, $action);

而且我无法从视图文件中访问角色:

$this->hasAccess('user', 'partner', 'news')
4

1 回答 1

0

在 application\plugins\Acl.php 中,您必须将该 ACL 实例附加到您的视图

class Application_Plugin_Acl extends Zend_Controller_Plugin_Abstract {
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
    $acl = new Zend_Acl();

    // Set ACL to view files
    $layout = Zend_Layout::getMvcInstance();
    $view   = $layout->getView();
    $view->acl = $acl;
}

然后,您可以在视图文件中使用 ACL:

$this->acl->hasAccess(...);

如果要在 View Helpers 中使用 ACL,请使用:

$this->view->acl->hasAccess(...);
于 2012-10-03T08:00:18.140 回答