Zend framework2 ZF2:控制器';s向前插件不';不行。如何让它工作?
我需要将ajax请求转发给当前控制器的其他操作方法。我使用转发插件,但它不工作。手册中有一个关于如何使用Forward插件的示例:Zend framework2 ZF2:控制器';s向前插件不';不行。如何让它工作?,zend-framework2,zend-framework-mvc,zend-framework-modules,zend-framework-routing,Zend Framework2,Zend Framework Mvc,Zend Framework Modules,Zend Framework Routing,我需要将ajax请求转发给当前控制器的其他操作方法。我使用转发插件,但它不工作。手册中有一个关于如何使用Forward插件的示例: $foo = $this->forward()->dispatch('foo', array('action' => 'process')); return array( 'somekey' => $somevalue, 'foo' => $foo, ); 我的代码: // From Ajax on the
$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)
如果我不使用Forward everything,则请求会转到索引操作
刚刚好。只有前进才会出错
:
要使Forward插件工作,调用它的控制器必须是
服务意识;否则,插件将无法检索
所请求控制器的已配置和注入实例
:
实现上述每个接口是冗余的一个教训;
你不会经常想这么做的。因此,我们开发了两个摘要,
可以扩展以开始的基本控制器
AbstractActionController实现以下每个接口:
Zend\Stdlib\DispatchableInterface
Zend\Mvc\InjectApplicationEventInterface
Zend\ServiceManager\ServiceLocatorAwareInterface
Zend\EventManager\EventManagerAwareInterface
因此,我的
FooController
扩展了AbstractActionController
,它实现了ServiceLocatorAwareInterface
,因此转发必须工作,但它不能工作。我错过了什么?如何使其工作?您应该记住,调度插件通过名称从服务管理器获取要调度到的控制器。因此,您应该使用正确的名称,而不仅仅是类名
在配置中查找controllers.invokables数组。应该包含服务的哪个名称映射到哪个FQCN
可能您的名字是FooController,那么忘记我刚才说的吧在调用控制器时,您应该使用完全限定的名称,因此“FooController”也应该使用名称空间。 此外,还应在模块配置文件中的可调用项列表中添加控制器,例如:
return array(
'controllers' => array(
'invokables' => array(
'FooController' => 'Namespace/Controller/FooController'
...
),
)
)
试试这个:
class FooController extends AbstractActionController {
public function indexAction() {
return $this->forward()->dispatch('Bar\Controller\Bar',
array(
'action' => 'process',
'somekey' => $somevalue,
));
}
}
class FooController extends AbstractActionController {
public function indexAction() {
return $this->forward()->dispatch('Foo',
array(
'action' => 'process',
'somekey' => $somevalue,
));
}
}
这里可以调用的是:'Bar\Controller\Bar'=>'Bar\Controller\Bar'
尝试以下操作:
class FooController extends AbstractActionController {
public function indexAction() {
return $this->forward()->dispatch('Bar\Controller\Bar',
array(
'action' => 'process',
'somekey' => $somevalue,
));
}
}
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
),
),
),
),
),
“控制器”=>阵列(
'invokables'=>数组(
'Foo'=>'Foo\Controller\FooController',//数组(
“路由”=>数组(
'foo'=>数组(
'类型'=>'段',
“选项”=>数组(
'route'=>'/foo[/:action][/:id],//数组(
“行动”=>“[a-zA-Z][a-zA-Z0-9_u-]*”,
“id'=>”[0-9]+',
),
“默认值”=>数组(
'controller'=>'Foo',//'index'//