无法基于 Zend_Acl 创建导航
我正在尝试学习使用 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天了。
I am trying to learn to create navigation with Zend_Acl.
But the navigation only displays for admin and no one else.
I have read through my code and I tried to trace the code that comes with ZendFramework. But I am stuck and I can't figure out what I am doing wrong.
Here is my acl class:
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' );
}
}
Here is the xml file that defines the navigation:
<?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>
And here is the code in the bootstrap file that sets up the navigation:
$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 holds the acl object and zend registry holds the role of the logged in user.
please ask any question you might have. I have been completely stuck for over 3 days.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我所看到并让我担心的是两件事。
首先是名为“guestbook”的资源被使用了三次。我认为这不是你的问题,但你应该解决这个问题。
其次,也是你的问题,allow 中的第三个参数是 $privileges。我不确定我现在在说什么,但导航不需要特权。不过,最肯定不是您提供的那些。
所以,试试这个:
What I see and concerns me are two things.
First is a resource named "guestbook" is used three times. I don't think this is your problem but you should fix that.
Second, and your problem, the third argument in allow are $privileges. I'm not certain about what I'm saying now but navigation does not require privileges. Most certainly not the ones you've provided, though.
So, try just this: