2

我需要將ajax請求轉發給當前控制器的其他Action方法。我使用Forward插件,但它不起作用。還有就是手冊中有關如何使用轉發插件的例子:ZF2:控制器的Forward插件不起作用。如何使它工作?

$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) 

如果我不要使用轉發一切正常,請求indexAction就好了。只有向前引發錯誤。

From the manual, about The Forward Plugin

對於正向插件工作時,控制器調用它必須是 ServiceLocatorAware;否則,該插件將無法檢索到所請求的控制器的配置和注入實例 。

From the manual, about Available Controllers

實施上述各接口中的是在冗餘的教訓; 你不會經常想要這樣做。因此,我們開發了兩個抽象的基礎控制器,您可以擴展以開始使用。

AbstractActionController實現以下每個接口:

的Zend \ STDLIB \ DispatchableInterface 的Zend \的mvc \ InjectApplicationEventInterface 的Zend \的ServiceManager \ ServiceLocatorAwareInterface 的Zend \ eventmanager進行\ EventManagerAwareInterface

所以我FooController延伸AbstractActionController ,它實現了ServiceLocatorAwareInterface,所以Forward必須工作,但它不會。我錯過了什麼?如何使它工作?

回答

3

您應該記住,dispatch插件可以讓控制器通過名稱從服務管理器分派。因此,您應該使用正確的名稱而不僅僅是類名。

查看您的配置中的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網絡le是這樣的:

'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 
        ), 
       ), 
      ), 
     ), 
    ),