问题描述
我正在尝试做类似以下问题的事情:
I am trying to do something like the following question:
但是,当我使用代码时(如答案中所推荐):
However, when I use the code (as recommended in the answer):
$event->setController('MyMainBundle:Manage:show');
我刚刚收到一个错误:
LogicException: The controller must be a callable (MyMainBundle:Manage:show given).
有没有办法在 setController 中使用 Bundle:Controller:Method 语法?或者我可以调用其他一些方法来将其解析为可调用"?
Is there a way to use that Bundle:Controller:Method syntax in setController? Or maybe some other method I can call to resolve that to a "callable"?
推荐答案
你应该给 $event->setController
的是一个可调用的.您给出的字符串表示可调用对象的逻辑路径.
What you should give to $event->setController
is a callable.What you give a string representing the logical path to a callable.
你可以使用 symfony 的 ControllerResolver 来解析这个字符串.
You can resolve this string using symfony's ControllerResolver.
你必须在你的监听器中注入 controller_resolver
服务,然后像这样使用它:
You have to inject the controller_resolver
service in your listener, and then use it like this:
$request = new Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_controller', 'MyMainBundle:Manage:show'));
$event->setController($this->resolver->getController($request));
但您显然在这里完成了框架的工作.
But you are clearly doing the framework's job here.
这篇关于Symfony2 使用 bundle:controller:action 符号在 kernelControllerEvent 中设置控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!