現在のコントローラーの他の Action メソッドに ajax リクエストを転送する必要があります。Forward プラグインを使用していますが、機能しません。Forward Plugin の使用方法については、マニュアルに例があります。
$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
。Forward のみがエラーをスローします。
The Forward Plugin についてのマニュアルから:
Forward プラグインが機能するには、それを呼び出すコントローラーが ServiceLocatorAware である必要があります。そうしないと、プラグインは、要求されたコントローラーの構成および挿入されたインスタンスを取得できません。
マニュアルから、 Available Controllers について:
上記の各インターフェイスを実装することは、冗長性の教訓です。あなたはしばしばそれをしたくないでしょう。そのため、開始するために拡張できる 2 つの抽象的な基本コントローラーを開発しました。
AbstractActionController は、次の各インターフェースを実装します。
Zend\Stdlib\DispatchableInterface Zend\Mvc\InjectApplicationEventInterface Zend\ServiceManager\ServiceLocatorAwareInterface Zend\EventManager\EventManagerAwareInterface
私のFooController
extendsAbstractActionController
は を実装ServiceLocatorAwareInterface
しているので、Forward は機能しなければなりませんが、機能しません。私は何を取りこぼしたか?それを機能させる方法は?