Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Set Controller in the kernelControllerEvent using bundle:controller:action notation

Tags:

symfony

I am trying to do something like the following question:

Trying to swap a controller using an event listener with Symfony2

However, when I use the code (as recommended in the answer):

$event->setController('MyMainBundle:Manage:show');

I just get an error:

LogicException: The controller must be a callable (MyMainBundle:Manage:show given).

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"?

like image 743
Matt Avatar asked May 18 '26 21:05

Matt


1 Answers

What you should give to $event->setController is a callable. What you give a string representing the logical path to a callable.

You can resolve this string using symfony's ControllerResolver.

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.

like image 96
Florian Klein Avatar answered May 22 '26 16:05

Florian Klein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!