我需要将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必须工作,但它不会。我错过了什么?如何使它工作?