March 28, 2016

listen to kernel.request event on Silex?

Lhassan Baazzi’s Question:

I want to listen to kernel.request event on Silex microframework http://silex.sensiolabs.org/documentation

How ?

You can access the dispatcher service as $app['dispatcher'] and you can see here how to use it. But I think you should use Silex’s before event instead as it’s called on kernel.request as well (here is a good expample how to use it).

UPDATE:

The links above are outdated. Before filters moved to a new middlewares section and here is how you can write one:

$app->before(function (Request $request) {
    // do what you want ...

    // if you want you can return a response so the controller won't be called
    // return new Response($content);

    // or just return null
});

There is a on($eventName, $callback, $priority = 0) method on silex application which you can use to listen to any event. On this particular case it will be as following.

$app->on(SymfonyComponentHttpKernelKernelEvents::REQUEST, function (SymfonyComponentHttpKernelEventGetResponseEvent $event) use ($app) {
    // Your actions
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!