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

前端 未结 5 685
有刺的猬
有刺的猬 2020-12-02 17:26

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

相关标签:
5条回答
  • 2020-12-02 17:51

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-02 17:52

    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.

    0 讨论(0)
  • 2020-12-02 17:56

    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);
        }
      }
    ?>
    
    0 讨论(0)
  • 2020-12-02 17:57

    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.

    0 讨论(0)
  • 2020-12-02 17:57

    I put into my AppController class the following method and variable so it is caches in case of multiple calls

    var $controllersArray = array();
    
    function _getController( $pControllerName ){
        if ( ! isset($this->controllersArray[$pControllerName]) ){
            $importRes = App::import('Controller', $pControllerName);// The same as require('controllers/users_controller.php');
            $strToEval = "\$controller = new ".$pControllerName."Controller;";
            $evalRes = eval($strToEval);
            if ( $evalRes === false ){
                throw new AppException("Error during eval of given getController '$pControllerName'");
            }
            $controller->constructClasses();// If we want the model associations, components, etc to be loaded
            $this->controllersArray[$pControllerName] = $controller;
        }
        $result = $this->controllersArray[$pControllerName];
    
        return $result;
    }
    
    0 讨论(0)
提交回复
热议问题