本文介绍了CakePHP:以数组为参数调用另一个控制器的动作的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个控制器中,调用另一个控制器的动作并将数组作为参数传递的最合适的方法是什么?

In a controller, what is the most appropriate way to call the action of another controller and also pass an array as parameter?

我知道您可以使用 requestAction 来调用其他控制器中的操作.但是是否可以使用请求操作将数组作为参数传递?

I know that you can use requestAction to call actions within other controllers. But is it possible to pass arrays as parameters using request action?

不,我不想将操作放在 App Controller 中.所以这不是我的解决方案.

And no, I do not want to put the action in the App Controller. So that is not a solution for me.

我知道的唯一另一种方法是加载另一个控制器,如下所述:http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Configuration.html#importing-controllers-models-components-behaviors-views-and-helpers

The only other way I know is to load the other controller as explained in:http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Configuration.html#importing-controllers-models-components-behaviors-views-and-helpers

但是有没有更简单的方法来在传递数组作为参数的同时调用其他控制器的动作?

But is there an easier way to just call the other controllers action while passing an array as parameter?

我是 cakePHP 的新手,所以任何建议都值得赞赏.谢谢.

I am new to cakePHP so any suggestion is appreciated. Thanks.

推荐答案

将逻辑从第二个控制器移到其模型中,然后在第一个控制器的操作中执行类似操作是否合适?

Would it be appropriate for you to move the logic from the second controller into its model, then do something like this in your first controller's action?

$var = ClassRegistry::init('SecondModel')->myMethod($array);
$this->set(compact('var'));

然后,在第一个控制器操作的视图中,您可以使用该数据.

Then, in the view for the first controller's action, you can use that data.

我总是尝试将控制器方法保留为您可以通过浏览器访问的操作,在我的模型中放置尽可能多的逻辑,从控制器操作调用外部模型方法,这些操作需要来自不是该控制器模型的模型的数据,然后在我的视图中使用该数据,如果是经常查看的数据,我会为其创建一个元素.

I always try to keep controller methods to actions you can hit through the browser, put as much logic in my models, call foreign model methods from controllers actions that need data from models that aren't the model for that controller, then use that data in my views, and if it's data that is viewed frequently, I create an element for it.

这篇关于CakePHP:以数组为参数调用另一个控制器的动作的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!