Zend:通过继承而不是组合进行 ACL 和 Auth
我读过几本不同的 Zend 书籍,其中 ACL 和 Auth 是使用组合实现的。它们通常涉及带有 preDispatch 方法的 Action Helpers,在引导程序中注册。
现在,在迄今为止的所有编码中,我的每个控制器都执行相同的 ACL 和 Auth 方法。因此我想我不妨使用继承来实现这一点。我错过了什么吗?继承似乎更简单一些。
早些时候,我使用 Action Helpers 来避免将所有规则加载到 ACL 对象中。 (我只是将当前控制器注册为 ACL 资源,并为其添加规则)。
然而,当我开始集成 Zend Navigation 时,我发现我需要所有 ACL 规则(无论控制器如何),以便确定向用户呈现哪些菜单选项。
我想通过组合,我的 ACL 和 Auth 帮助程序在其他项目上更容易使用,但这对我来说只是一个小小的好处。
谢谢!
I have read a few different Zend books where ACL and Auth are implemented using composition. They generally involve Action Helpers with preDispatch methods, registered in the bootstrap.
Now, in all of the coding so far, the same ACL and Auth methods are executed for each of my controllers. Therefore I am thinking that I might just as well use inheritance to accomplish this. Am I missing something? Inheritance just seems a bit more straightforward.
Earlier on I was using Action Helpers to avoid having to load all the rules into the ACL object. (I was just registering the current controller as the ACL resource, and adding the rules for that).
When I came to integrate Zend Navigation, however, I found that I needed all the ACL rules (irrespective of controller), in order to work out which menu options to present to the user.
I guess that with composition, my ACL and Auth helpers are easier to use on other projects, but that's only a small benefit in my case.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以使用控制器插件,请参阅以前的答案,看起来像一个组合,但非常接近控制器的继承。这并不妨碍您拥有可用于菜单的 Zend_Acl 对象。
You could use controllers plugins as well, see previous answer, seems like a composition but is very near inheritance of controllers. And that does not prevent you from having a Zend_Acl object available for menus.
一般来说,有很多充分的理由选择组合而不是继承。您可以在这里看到很多意见:更喜欢组合而不是继承?
也就是说,这取决于您可以决定是否有更好的理由使用继承而不是组合。您可以将 ACL 集成到基本控制器类中,并使所有控制器都继承它。您在这里还有另一个问题: Zend 中所有控制器的基本控制器框架
希望有帮助,
There are many valid reasons to prefer composition over inheritance, in general. You can see a lot of opinions here: Prefer composition over inheritance?
That said, it's up to you to decide if you have better reasons to use inheritance instead of composition. You can integrate your ACL in a base controller class and make all your controllers inherit from it. You have another SO question about it here: Base controller for all controllers in zend framework
Hope that helps,