CakePHP: best way to call an action of another controller with array as parameter?

83,779

Solution 1

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.

Solution 2

I would not advice to use the method requestAction but rather import, and instantiate the needed controller.

CakePHP doc says about requestAction that:

"It is rarely appropriate to use in a controller or model"

http://book.cakephp.org/view/434/requestAction

Once you imported and loaded the controller you can call any method of this controller with its parameters.

<?php
  //Import controller
  App::import('Controller', 'Posts');

  class CommentsController extends AppController {
    //Instantiation
    $Posts = new PostsController;
    //Load model, components...
    $Posts->constructClasses();

    function index($passArray = array(1,2,3)) {
      //Call a method from PostsController with parameter
      $Posts->doSomething($passArray);
    }
  }
?>

Solution 3

As of CakePHP 1.2.5, you should be able to pass various parameter types through the second parameter in requestAction(). e.g.:

$this->requestAction('/users/view', array('pass' => array('123')));

Then in the UsersController:

function view($id) {
    echo $id; // should echo 123 I believe, otherwise try $this->params['pass'].
}

Instead of using 'pass' above, you can alternatively try 'form' and 'named' to pass form/named parameters respectively.

Solution 4

CakePHP 2.X:

<?php
App::uses('AppController', 'Controller');
App::uses('PostsController', 'Controller');

class CommentsController extends AppController {

    public function index($parameter = null){
        //Instantiate
        $Posts = new PostsController();
        //Load model, components...
        $Posts->constructClasses();

        //Call a method of Posts passing a parameter
        $Posts->aMethod($parameter);
    }
}
Share:
83,779
Vicer
Author by

Vicer

Updated on May 29, 2020

Comments

  • Vicer
    Vicer almost 4 years

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

    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?

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

    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?

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