CakePHP : 매개 변수로 배열을 가진 다른 컨트롤러의 동작을 호출하는 가장 좋은 방법?

StackOverflow https://stackoverflow.com/questions/1507552

문제

컨트롤러에서 다른 컨트롤러의 동작을 호출하고 배열을 매개 변수로 전달하는 가장 적절한 방법은 무엇입니까?

RequestAction을 사용하여 다른 컨트롤러 내에서 작업을 호출 할 수 있습니다. 그러나 요청 조치를 사용하여 배열을 매개 변수로 전달할 수 있습니까?

그리고 아니요, 앱 컨트롤러에 작업을 넣고 싶지 않습니다. 그래서 그것은 나에게 해결책이 아닙니다.

내가 아는 유일한 방법은 다음과 같이 설명 된대로 다른 컨트롤러를로드하는 것입니다.http://book.cakephp.org/1.3/en/the-manual/developing-with-cakephp/configuration.html#importing-controllers-components-behaviors-vehaviors-ne-helpers

그러나 배열을 매개 변수로 전달하는 동안 다른 컨트롤러 작업을 호출하는 더 쉬운 방법이 있습니까?

나는 CakePHP를 처음 사용하므로 모든 제안에 감사드립니다. 감사.

도움이 되었습니까?

해결책

두 번째 컨트롤러에서 논리를 모델로 옮기고 첫 번째 컨트롤러의 동작에서 이와 같은 작업을 수행하는 것이 적절합니까?

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

그런 다음 첫 번째 컨트롤러 작업을 볼 때 해당 데이터를 사용할 수 있습니다.

나는 항상 브라우저를 통해 발생할 수있는 작업에 컨트롤러 방법을 유지하려고 노력하고, 모델에 많은 논리를 넣고, 해당 컨트롤러의 모델이 아닌 모델의 데이터가 필요한 컨트롤러 작업에서 외국 모델 메소드를 호출 한 다음 해당 데이터를 사용하십시오. 내 견해와 자주 보는 데이터라면 요소를 만듭니다.

다른 팁

메소드 요청 actions를 사용하는 것이 아니라 오히려 필요한 컨트롤러를 인스턴스화하는 것이 좋습니다.

Cakephp Doc은 요청에 대해 다음과 같이 말합니다.

"컨트롤러 나 모델에서 사용하는 것은 거의 적절하지 않습니다."

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

컨트롤러를 가져오고로드 한 후에는 매개 변수 로이 컨트롤러의 모든 메소드를 호출 할 수 있습니다.

<?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);
    }
  }
?>

CakePhp 1.2.5에서 요청 Action ()의 두 번째 매개 변수를 통해 다양한 매개 변수 유형을 전달할 수 있어야합니다. 예 :

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

그런 다음 UsersController에서 :

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

위의 'Pass'를 사용하는 대신 '양식'및 '명명'을 시도하여 양식/명명 매개 변수를 각각 통과 할 수 있습니다.

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);
    }
}

AppController 클래스에 다음 방법과 변수를 여러 통화시 캐시입니다.

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top