Zend框架ACL问题

发布于 2024-10-21 05:26:50 字数 1360 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-10-28 05:26:50

一般来说,将此类内容(不仅仅是 ACL)放入其自己的类中可以带来多种好处,包括:

  1. 可测试性:您现在拥有一个可以应用单元测试的组件。
  2. 可扩展性:如果需要,您可以扩展它并修改它
  3. 可移植性:只需将其放入另一个项目中即可使用它。

在这种特殊情况下,这些可能看起来不那么明显。在更复杂的情况下,这些好处变得更加明显。但与大多数事情一样,YMMV。

顺便说一句,我还发现——有时仍然发现——学习 ZF 是一种攀登。但我意识到我不仅学习了框架,还学习了很多最佳实践,如依赖注入、单元测试、DRY、SRP、设计模式等。我建议你坚持下去;这是非常值得的。祝你好运!

Generally speaking, putting this kind of stuff - not just ACL - in its own class provides several benefits, including:

  1. Testability: You now have a single component to which you can apply unit testing.
  2. Extensibility: You can extend it and modify it, if necessary
  3. Portability: Use it in another project simply by dropping it in.

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!

月朦胧 2024-10-28 05:26:50

我认为它做同样的事情 - 第一个只是从“外部”更干净:)

编辑:不,它不干净 - 它只是分开......我认为你可以选择你将如何做......我个人投票支持引导解决方案:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文