【问题标题】:Could not create navigation based on Zend_Acl无法基于 Zend_Acl 创建导航
【发布时间】:2011-07-04 16:29:57
【问题描述】:

我正在尝试学习使用 Zend_Acl 创建导航。 但是导航只显示给管理员,没有其他人。

我已经阅读了我的代码,并尝试跟踪 ZendFramework 附带的代码。但是我被卡住了,我无法弄清楚我做错了什么。

这是我的 acl 类:

class Application_Model_LibraryACL extends Zend_Acl
{
    public function __construct()
    { 
            $this->add(new Zend_Acl_Resource( 'guestbook' ) );
            $this->add( new Zend_Acl_Resource( 'index' ) );

            $this->add(new Zend_Acl_Resource( 'error' ) );
            $this->add(new Zend_Acl_Resource( 'authentication' ) );
            $this->add(new Zend_Acl_Resource( 'login' ), 'authentication' );
            $this->add(new Zend_Acl_Resource( 'logout' ), 'authentication' );

            $this->addRole( new Zend_Acl_Role( 'guest' ) );
            $this->addRole( new Zend_Acl_Role( 'member' ), 'guest' );
            $this->addRole( new Zend_Acl_Role( 'admin' ), 'member' );

            $this->allow( 'guest', 'error', 'error' );
            $this->allow( 'guest', 'index', 'index' );
            $this->allow( 'guest', 'authentication', array( 'login', 'logout' ) );
            $this->allow( 'member', 'guestbook', 'sign' );
            $this->allow( 'admin' );
    } 
}

这是定义导航的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <nav>
            <home>
                    <label>Home</label>
                    <controller>index</controller>
                    <action>index</action>
                    <resource>index</resource>
            </home>

            <logout>
                    <label>Logout</label>
                    <controller>authentication</controller>
                    <action>logout</action>
                    <resource>logout</resource>
            </logout>

            <login>
                    <label>Login</label>
                    <controller>authentication</controller>
                    <action>login</action>
                    <resource>login</resource>
            </login>

            <guestbook>
                    <label>Guestbook</label>
                    <resource>guestbook</resource>
                    <uri></uri>
                    <pages>
                            <list>
                                    <label>List</label>
                                    <controller>guestbook</controller>
                                    <action>index</action>
                                    <resource>guestbook</resource>
                            </list>
                            <sign>
                                    <label>Sign</label>
                                    <controller>guestbook</controller>
                                    <action>sign</action>
                                    <resource>guestbook</resource>
                            </sign>
                    </pages>
            </guestbook>


    </nav>

下面是设置导航的引导文件中的代码:

$navContainerConfig = new Zend_Config_Xml( APPLICATION_PATH . '/configs/navigation.xml', 'nav' );
$navContainer = new Zend_Navigation( $navContainerConfig );
$view->navigation( $navContainer )->setAcl( $this->acl )->setRole( Zend_Registry::get( 'role' ) );

$this->acl 持有 acl 对象,而 zend 注册表持有登录用户的角色。

请提出您可能有的任何问题。我已经完全被卡住了超过3天。

【问题讨论】:

  • 只要您使用管理员进行导航,该部分应该没问题。我要问的第一个问题是关于 setRole() 和您的 Zend_Registry 对象。你能确认(调试)角色正在改变吗?
  • 是的,它正在改变。我也试过 $view->navigation( $navContainer )->setAcl( $this->acl )->setRole( 'guest' );它仅在我传入“admin”时才有效
  • 我的测试环境已经启动,我的回答确实应该可以解决您的问题。您的允许语句中的第三个参数是罪魁祸首。

标签: zend-framework zend-navigation zend-acl


【解决方案1】:

我看到和关心的是两件事。

首先是一个名为“guestbook”的资源被使用了 3 次。我认为这不是你的问题,但你应该解决它。

第二,你的问题,allow 中的第三个参数是 $privileges。我不确定我现在在说什么,但导航不需要特权。不过,肯定不是您提供的那些。

所以,试试这个:

$this->allow( 'guest', 'error' );
$this->allow( 'guest', 'index' );
$this->allow( 'guest', 'authentication');
$this->allow( 'member', 'guestbook' );
$this->allow( 'admin' );

【讨论】:

  • 感谢您的帮助。但它不起作用。我又从头开始了。希望这次我能解决问题。
  • 奇怪,它对我有用。我的配置遇到了同样的问题,它适用于我发布的代码。除了我写的内容外,我真的没有在您的配置中看到任何错误。仅供参考:我为要测试的角色使用了确切的名称,而不是 Zend_Registry
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
相关资源
最近更新 更多