Zend Framework:控制器插件与操作助手
有人可以提供一些提示和/或示例来说明控制器插件和操作助手有何不同吗?是否存在这样的情况:某一特定任务可以由一个人完成,但另一个人却无法完成?对我来说,它们看起来或多或少相同,我经常难以决定何时使用什么......有什么大的区别吗?
Could someone give few tips and/or examples how Controller Plugins and Action Helpers are different? Are there situations where particular task could be accomplished with one but not another? For me they both look more or less the same and I'm often having trouble having to decide when to use what... Are there any big differences?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
控制器插件可以在路由过程中的任何点(preDispatch、postDispatch、routeStartup、routeShutdown)连接到任何控制器,这使得它们适合提供 ACL 强制等幕后功能。
操作助手用于控制器可能需要访问的可重用但可选的段(重定向器、flashMessenger)。
因此,如果您要创建始终需要自行执行的可重用代码片段,请使用控制器插件,否则您可能需要一个操作助手。
Controller plugins can hook into any controller at any point in the routing process (preDispatch postDispatch, routeStartup, routeShutdown) which makes them apt at providing behind the scenes functionality like ACL enforcement.
Action Helpers are for for reusable but optional segments that your controller might need to access (redirector, flashMessenger).
So if you are creating a reusable snippet of code that always needs to execute itself then use a controller plugin, otherwise you probably want an action helper.
您可以这样想:
所以问问自己,我是否有一个我希望能够从控制器中的所有操作中调用的方法?或者我是否需要向路由/调度过程添加逻辑。
您还可以查看 内置动作助手。
You can think of it this way:
So ask yourself, do I have a method that I would like to be able to call from all actions in my controller? Or do I need to add logic to the routing / dispatch process.
You might also have a look at the the Built in Action Helpers.
一张图说明了插件和动作助手之间的区别:
ZF 序列流
A picture to illustrate the difference between plugins and action helpers:
ZF Sequence Flow
操作助手还可以访问正在执行的实际控制器对象。控制器插件只能访问 FrontController,因此只能访问控制器和操作名称。
您使用哪个取决于您需要什么上下文。例如,如果您需要访问附加到控制器的视图对象,您将需要一个动作助手。
Action helpers also have access to the actual controlller object that's being executed. Controller Plugins only have access to the FrontController, and therefore only the controller and action name.
Which you use depends on what context you need. If you need to access a view object attached to a controller, for example, you will want an Action Helper.
另请注意,在前端控制器生命周期过程中,插件比操作助手首先获得控制(或调用)。
Also notice that, in the front controller life-cycle process, the plugins get the control(or invoked) first than the action helpers.