Zend: ACL and Auth via inheritance, not composition
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开发者_JAVA技巧 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!
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.
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,
精彩评论