Zend Framework:控制器插件与操作助手

发布于 2024-08-25 03:12:05 字数 122 浏览 8 评论 0原文

有人可以提供一些提示和/或示例来说明控制器插件和操作助手有何不同吗?是否存在这样的情况:某一特定任务可以由一个人完成,但另一个人却无法完成?对我来说,它们看起来或多或少相同,我经常难以决定何时使用什么......有什么大的区别吗?

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

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

发布评论

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

评论(5

忆离笙 2024-09-01 03:12:05

控制器插件可以在路由过程中的任何点(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.

脱离于你 2024-09-01 03:12:05

您可以这样想:

  • 操作助手用于向控制器添加方法。
  • 控制器插件用于向控制器添加路由/调度逻辑。

所以问问自己,我是否有一个我希望能够从控制器中的所有操作中调用的方法?或者我是否需要向路由/调度过程添加逻辑。

您还可以查看 内置动作助手

You can think of it this way:

  • Action helpers are used to add methods to controllers.
  • Controller plugins are used to add routing / dispatch logic to controllers.

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.

燃情 2024-09-01 03:12:05

一张图说明了插件和动作助手之间的区别:
ZF 序列流

A picture to illustrate the difference between plugins and action helpers:
ZF Sequence Flow

你列表最软的妹 2024-09-01 03:12:05

操作助手还可以访问正在执行的实际控制器对象。控制器插件只能访问 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.

情话墙 2024-09-01 03:12:05

另请注意,在前端控制器生命周期过程中,插件比操作助手首先获得控制(或调用)。

Also notice that, in the front controller life-cycle process, the plugins get the control(or invoked) first than the action helpers.

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