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

前端 未结 5 695
有刺的猬
有刺的猬 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: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;
    }
    

提交回复
热议问题