Symfony2使用bundle:controller:action notation在kernelControllerEvent中设置Controller

Mat*_*att 3 symfony

我想尝试做类似以下问题:

尝试使用Symfony2的事件侦听器交换控制器

但是,当我使用代码时(如答案中所建议的那样):

$event->setController('MyMainBundle:Manage:show');
Run Code Online (Sandbox Code Playgroud)

我刚收到一个错误:

LogicException: The controller must be a callable (MyMainBundle:Manage:show given).
Run Code Online (Sandbox Code Playgroud)

有没有办法在setController中使用Bundle:Controller:Method语法?或者也许我可以调用其他方法将其解析为"可调用"?

Flo*_*ian 5

你应该给予的$event->setController是可赎回的.你给出一个表示可调用的逻辑路径的字符串.

您可以使用symfony的ControllerResolver解析此字符串.

你必须controller_resolver在你的监听器中注入服务,然后像这样使用它:

$request = new Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_controller', 'MyMainBundle:Manage:show'));
$event->setController($this->resolver->getController($request));
Run Code Online (Sandbox Code Playgroud)

但是你明显在这里做框架的工作.