0

나는 다른 컨트롤러의 액션 메소드에서 값을 얻기 위해 하나 개의 컨트롤러의 액션 메소드에서 the Forward plugin를 호출ZF2 - 앞으로 플러그인은 ViewModel 객체를 반환합니다. 다른 값을 반환하는 방법 (예 : 단순하거나 연관 배열?

namespace Foo/Controller; 

class FooController { 

    public function indexAction() { 

     // I expect the $result to be an associative array, 
     // but the $result is an instance of the Zend\View\Model\ViewModel 
     $result = $this->forward()->dispatch('Boo/Controller/Boo', 
               array(
                'action' => 'start' 
              )); 
    } 
} 

을 그리고 여기 Boo 컨트롤러 나 적용의 :

namespace Boo/Controller; 

class BooController { 

    public function startAction() { 

     // I want this array to be returned, 
     //  but an instance of the ViewModel is returned instead 
     return array(
      'one' => 'value one', 
      'two' => 'value two', 
      'three' => 'value three', 
     ); 
    } 
} 

내가 print_r($result) 경우가있다 error/404 페이지의 ViewModel :

Zend\View\Model\ViewModel Object 
(
    [captureTo:protected] => content 
    [children:protected] => Array 
     (
     ) 

    [options:protected] => Array 
     (
     ) 

    [template:protected] => error/404 
    [terminate:protected] => 
    [variables:protected] => Array 
     (
      [content] => Page not found 
      [message] => Page not found. 
      [reason] => error-controller-cannot-dispatch 
     ) 

    [append:protected] => 
) 

모자는 계속하고 있니? 이 동작을 변경하고 the Forward plugin에서 필수 데이터 형식을 얻는 방법?

UPD 1

이제이 here 발견을 위해 :

MVC의는 이 자동화 컨트롤러에 대한 청취자의 몇 가지를 등록합니다. 첫 번째는 컨트롤러에서 연관 배열 을 반환했는지 확인합니다. 그렇다면 뷰 모델을 생성하고 연관 배열을 변수 컨테이너로 만듭니다. 이 View Model 다음에 이 MvcEvent의 결과를 대체합니다.

그리고이 작동하지 않습니다는 :

대신 내가 ViewModel에 변수를 넣어이 단지 배열을 얻는 수단
$this->getEvent()->setResult(array(
       'one' => 'value one', 
       'two' => 'value two', 
       'three' => 'value three', 
      )); 

return $this->getEvent()->getResult(); // doesn't work, returns ViewModel anyway 

하는 ViewModel을 반환하고 ViewModel에서 그 변수를 얻을. 아주 좋은 디자인, 나는 말할 수있다.

답변

2

ZF2의 동작에서보기를 비활성화해야합니다. 다음과 같이이 작업을 수행 할 수 있습니다.

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 

class IndexController extends AbstractActionController 
{ 
    public function indexAction() 
    { 
     $result = $this->forward()->dispatch('Application/Controller/Index', array('action' => 'foo')); 
     print_r($result->getContent()); 
     exit; 
    } 

    public function fooAction() 
    { 
     $response = $this->getResponse(); 
     $response->setStatusCode(200); 
     $response->setContent(array('foo' => 'bar')); 
     return $response; 
    } 
}