2

ajax 요청을 현재 컨트롤러의 다른 Action 메소드로 전달해야합니다. Forward 플러그인을 사용하지만 작동하지 않습니다. 앞으로 플러그인을 사용하는 방법에 대한 설명서의 예제가있다 :ZF2 : 컨트롤러의 전달 플러그인이 작동하지 않습니다. 어떻게 작동 시키는가?

$foo = $this->forward()->dispatch('foo', array('action' => 'process')); 
return array(
    'somekey' => $somevalue, 
    'foo'  => $foo, 
); 

내 코드 :

// From Ajax on the page. I apply to the indexAction of FooController, 
// I use RegEx route 
xhr.open('get', '/fooindex', true); 


// My Controller 
namespace Foo\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

// I extend the AbstractActionController, the manual says it's important for the Forward Plugin to work 
class FooController extends AbstractActionController { 

    // This is the action I send my request from Ajax 
    public function indexAction() { 

     // if the request if Ajax request I forward the run to the nextAction method 
     if ($this->getRequest()->isXmlHttpRequest()) { 

      // I do as manual says 
      $rs = $this->forward()->dispatch('FooController', array('action' => 'next')); 
     } 
    } 


    public function nextAction() { 

     // And I just want to stop here to see that the Forward Plugin works 
     // But control doesn't reach here 
     exit('nextAction'); 
    } 
} 

내가 콘솔에서 얻을 오류는 다음과 같습니다

GET http://test.localhost/fooindex 500 (Internal Server Error) 

경우 I 앞으로 모든 것을 잘 사용하지 마십시오. 요청은 indexAction에 그대로 있습니다. 전달에서만 오류가 발생합니다.

From the manual, about The Forward Plugin :

앞으로 플러그인이 작동하려면, 그것은 ServiceLocatorAware해야 호출하는 컨트롤러; 그렇지 않으면, 플러그인은 요청 된 컨트롤러의 구성되고 삽입 된 인스턴스 을 검색 할 수 없습니다.

From the manual, about Available Controllers : 상기 각 인터페이스 구현

중복성의 교훈; 자주하지 않으려합니다. 따라서 시작하기 위해 확장 할 수있는 두 개의 추상적 인 기본 컨트롤러를 개발했습니다.

AbstractActionController는 다음 각 인터페이스 구현 :

젠드 \ STDLIB \ DispatchableInterface 젠드 \ MVC \ InjectApplicationEventInterface 젠드 \ ServiceManager에 \ ServiceLocatorAwareInterface 젠드 \ 개의 EventManager \ EventManagerAwareInterface을

그래서 내 FooControllerAbstractActionController를 확장 , 이는 ServiceLocatorAwareInterface을 구현하므로 전달이 작동해야하지만 그렇지 않습니다. 내가 놓친 게 무엇입니까? 어떻게 작동 시키는가?

답변

3

디스패치 플러그인은 이름으로 서비스 관리자에서 디스패치하는 컨트롤러를 가져옵니다. 따라서 클래스 이름뿐만 아니라 올바른 이름을 사용해야합니다.

구성에서 controllers.invokables 배열을 찾습니다. 여기에는 서비스의 이름이 FQCN에 매핑되어야합니다.

그것은 당신 이름이 될 수는 FooController입니다, 나는 그냥 컨트롤러를 호출 할 때

1

당신은뿐만 아니라 네임 스페이스해야하므로 'FooController'을 정규화 된 이름을 사용해야합니다 말을 잊는다.또한 , 당신은 예를 들어, 모듈 설정 파일의 invokables 목록에 컨트롤러를 추가해야합니다 :

return array(
    'controllers' => array(
     'invokables' => array(
      'FooController' => 'Namespace/Controller/FooController' 
       ... 
     ), 
    ) 
) 
1

을이 시도 : 여기

class FooController extends AbstractActionController { 
    public function indexAction() { 
     return $this->forward()->dispatch('Bar\Controller\Bar', 
      array(
       'action' => 'process', 
       'somekey' => $somevalue, 
      )); 
    } 
} 

을 호출 가능한 것입니다 : 'Bar\Controller\Bar' => 'Bar\Controller\Bar'

1

시도가 이 :

class FooController extends AbstractActionController { 
    public function indexAction() { 
     return $this->forward()->dispatch('Foo', 
      array(
       'action' => 'process', 
       'somekey' => $somevalue, 
      )); 
    } 
} 

귀하의 module.config.php fi 르는 다음과 같습니다 :

'controllers' => array(
    'invokables' => array(
     'Foo' => 'Foo\Controller\FooController', // <----- Module Controller 
    ), 
), 

    'router' => array(
     'routes' => array(
      'foo' => array(
       'type' => 'segment', 
       'options' => array(
        'route' => '/foo[/:action][/:id]', // <---- url format module/action/id 
        'constraints' => array(
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'id'  => '[0-9]+', 
        ), 
        'defaults' => array(
         'controller' => 'Foo', // <--- Defined as the module controller 
         'action'  => 'index',     // <---- Default action 
        ), 
       ), 
      ), 
     ), 
    ),