2

現在のコントローラーの他の 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

私のFooControllerextendsAbstractActionControllerは を実装ServiceLocatorAwareInterfaceしているので、Forward は機能しなければなりませんが、機能しません。私は何を取りこぼしたか?それを機能させる方法は?

4

4 に答える 4

3

ディスパッチ プラグインは、ディスパッチ先のコントローラーをサービス マネージャーから名前で取得することに注意してください。したがって、クラス名だけでなく、正しい名前を使用する必要があります。

controllers.invokables 配列の構成を調べます。これには、サービスのどの名前がどの FQCN にマップされるかが含まれている必要があります。

IS FooController と名付けたのかもしれませんが、今言ったことは忘れてください。

于 2012-11-20T14:55:42.293 に答える
1

これを試して:

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

module.config.php ファイルは次のようになります。

'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
                    ),
                ),
            ),
        ),
    ),
于 2013-12-18T08:27:09.007 に答える
1

コントローラーを呼び出すときは完全修飾名を使用する必要があるため、「FooController」も名前空間にする必要があります。また、モジュール構成ファイルの invokables のリストにコントローラーを追加する必要があります。次に例を示します。

return array(
    'controllers' => array(
        'invokables' => array(
             'FooController' => 'Namespace/Controller/FooController'
              ...
         ),
    )
 )
于 2013-02-05T14:00:00.693 に答える
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'

于 2013-04-18T11:11:48.653 に答える