从 ZF2 中的控制器调用另一个控制器操作的最佳实践 [英] Best practice to call another controller action from a controller in ZF2

查看:26
本文介绍了从 ZF2 中的控制器调用另一个控制器操作的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用另一个控制器的动作的最佳方式是什么?

What is the best way of calling an action of another controller?

我正在考虑不同的选择:

I am thinking in different options:

  1. 使用转发插件
  2. 将第二个控制器设置为第一个控制器中的依赖项

我认为第一个选项可行,但我想知道第二个选项是否有意义.在这种情况下,

I think the first option will work but I would like to know if the second one has sense. In that case,

  1. 如何获取控制器依赖项?

我尝试使用服务管理器获取它,但它不存在.我尝试为控制器创建一个工厂,并从控制器管理器中获取另一个控制器,当您实现 FactoryInterface 时,该控制器作为参数传递给 createService 方法.但它说那里没有控制器.与控制器存在不同的定位器,而不能以与服务定位器相同的方式使用?

I have tried to get it using the Service Manager but it isn't there. I have tried creating a Factory for the controller and get the another controller from the Controller Manager which is passed as an argument to the createService method when you implement the FactoryInterface. But it says that no controller is there. Exists a different locator from controllers than can be used in the same way as the Service Locator?

  1. 具有依赖作用并将被调用的控制器必须扩展或实现任何特定的类或接口?

推荐答案

我会说使用 forward 插件 是 ZF2 的方式.您提出的另一个解决方案听起来非常复杂,可能会让您遇到各种意想不到的 dito 错误问题.

I would say that using the forward plugin is the ZF2 way to do it. This other solution that you propose sounds very complicated and might get you in all sorts of unsuspected trouble with dito bugs.

$this->forward()->dispatch('foo', array('action' => 'process'));

或者是否有合理的论据为什么使用这个 forward 插件不是你想要的?

Or is there a reasonable argument why using this forward plugin is not what you want?

我实际上不明白为什么您不能从 ControllerManager 获取其他控制器.您确定它已注册并且您使用正确的名称来获取它吗?

I actually don't understand why you can't get your other controller from your ControllerManager. Are you sure it is registered and that you use the correct name to get it?

这篇关于从 ZF2 中的控制器调用另一个控制器操作的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆