I have a simple silex application and using a lot of different externals controllers.
I am using register and mount to connect it to my application.

$app->register($externalController = new ExternalController());
$app->mount('/control', $externalController );

It adds the routes login, logout, etc in its service provider class:

$controllers->get('/start', 'user.controller:loginAction')
    ->bind('control.start');

I want to add an event or middleware listener to actions provided by it.

I have searched the silex and symfony documentation, but I didn't find an easy way.
I have tried to use $app['controllers'], but this returns a ControllerCollection without any possibility to change something (or I didn't understand it).

What is the recommended way to add a new listener to an existing non self written controller?

有帮助吗?

解决方案

I have found one possible way in the meantime to flush the controllers to create the RouteCollection and retrieve it via binding name.
You will receive a Route instance and can use there the normal middleware listener methods like before, after and so on.

$app->flush();
$route = $app['routes']->get('control.start');
$route->before(function(Symfony\Component\HttpFoundation\Request $request) use ($app) {
    throw new RuntimeException('You should see me.');
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top