Zend框架ACL问题
我是 Zend 框架的新手,目前正在研究 Zend_Acl 。 网上有很多例子。在很多这样的示例中,您会看到类似这样的代码:
class My_Acl extends Zend_Acl {
public function __construct() {
//Add a new role called "guest"
$this->addRole(new Zend_Acl_Role('guest'));
//Add a role called user, which inherits from guest
$this->addRole(new Zend_Acl_Role('user'), 'guest');
//Add a resource called page
$this->add(new Zend_Acl_Resource('page'));
//Add a resource called news, which inherits page
$this->add(new Zend_Acl_Resource('news'), 'page');
//Finally, we want to allow guests to view pages
$this->allow('guest', 'page', 'view');
//and users can comment news
$this->allow('user', 'news', 'comment');
}
}
基本上 - 我们扩展 Zend_Acl 类,在其中定义角色和资源。我有点不明白为什么我们要创建单独的类,而不是在引导程序中的资源方法中做同样的事情,然后将结果推入注册表?
就像这样:
protected function _initAcl()
{
$myacl = new Zend_Acl();
$myacl->addRole(new Zend_Acl_Role('guest'));
$myacl->addRole(new Zend_Acl_Role('user'), 'guest');
$myacl->add(new Zend_Acl_Resource('page'));
$myacl->add(new Zend_Acl_Resource('news'), 'page');
$myacl->allow('guest', 'page', 'view');
$myacl->allow('user', 'news', 'comment');
Zend_Registry::set('acl', $myacl);
}
我认为这些方法会给我们带来相同的结果,对吗?
谢谢! ps该死的zend很复杂
I'm new to Zend framework and currently looking at Zend_Acl .
There are multiple examples online. In a lot of these example you would see code like this one:
class My_Acl extends Zend_Acl {
public function __construct() {
//Add a new role called "guest"
$this->addRole(new Zend_Acl_Role('guest'));
//Add a role called user, which inherits from guest
$this->addRole(new Zend_Acl_Role('user'), 'guest');
//Add a resource called page
$this->add(new Zend_Acl_Resource('page'));
//Add a resource called news, which inherits page
$this->add(new Zend_Acl_Resource('news'), 'page');
//Finally, we want to allow guests to view pages
$this->allow('guest', 'page', 'view');
//and users can comment news
$this->allow('user', 'news', 'comment');
}
}
So basically - we extend our Zend_Acl class where we define roles and resources. I'm sorta failing to understand why would we create separate class versus doing the same thing in for example resource method in the bootstrap and then shoving the result into registry?
Like this for ex:
protected function _initAcl()
{
$myacl = new Zend_Acl();
$myacl->addRole(new Zend_Acl_Role('guest'));
$myacl->addRole(new Zend_Acl_Role('user'), 'guest');
$myacl->add(new Zend_Acl_Resource('page'));
$myacl->add(new Zend_Acl_Resource('news'), 'page');
$myacl->allow('guest', 'page', 'view');
$myacl->allow('user', 'news', 'comment');
Zend_Registry::set('acl', $myacl);
}
Am I right in thinking that these ways will give us the same result?
thanks!
p.s. damn zend is complicated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,将此类内容(不仅仅是 ACL)放入其自己的类中可以带来多种好处,包括:
在这种特殊情况下,这些可能看起来不那么明显。在更复杂的情况下,这些好处变得更加明显。但与大多数事情一样,YMMV。
顺便说一句,我还发现——有时仍然发现——学习 ZF 是一种攀登。但我意识到我不仅学习了框架,还学习了很多最佳实践,如依赖注入、单元测试、DRY、SRP、设计模式等。我建议你坚持下去;这是非常值得的。祝你好运!
Generally speaking, putting this kind of stuff - not just ACL - in its own class provides several benefits, including:
In this particular case, these might not seem so evident. In a more complex case, these benefits become more apparent. But as with most things, YMMV.
BTW, I also found - and sometime still do find - learning ZF to be a climb. But I realize that I was learning not just the framework, but also lots of best practices like dependency injection, unit-testing, DRY, SRP, design patterns, etc. I advise you keep at it; it's well worth it. Good luck!
我认为它做同样的事情 - 第一个只是从“外部”更干净:)
编辑:不,它不干净 - 它只是分开......我认为你可以选择你将如何做......我个人投票支持引导解决方案:)
I think that it do the same thing - the first is just cleaner from "outside" :)
EDIT: no, it's not cleaner - it's just separated ... I think that you can choose how you will do it ... I personaly vote for the bootstrap solution :)