Reputation: 5607
I am trying to set up a simple event listener to execute some code at the start of and before every request. I'm just trying to echo out a simple message to check it's all working.
If I make a mistake (e.g. a typo) or wrongly configure (like I have for the last 30 minutes) then it returns various error messages. But I think I have it setup as the error messages have gone.
This is my code:
I have added this to my /app/config.yml file
services:
kernel.listener.request_listener:
class: Acme\Bundle\NewBundle\EventListener\RequestListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelRequest }
And this is the code in the related file
namespace Acme\Bundle\NewBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
class RequestListener
{
public function onKernelRequest(GetResponseEvent $event)
{
$response = new Response();
$response->setContent("hello");
$event->setResponse($response);
}
}
Upvotes: 0
Views: 237
Reputation: 4598
I believe you have hooked into a wrong event kernel.exception
, which is called/dispatched only when exception occurs.
I believe you should have hooked the kernel.request
event,
services:
kernel.listener.request_listener:
class: Acme\Bundle\NewBundle\EventListener\RequestListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
kernel.request
is called/dispatched on every request, and before starting to handle a request.
http://symfony.com/doc/2.0/book/internals.html#handling-requests
Before doing anything else, the kernel.request event is notified -- if one of the listeners returns a Response, it jumps to step 8 directly;
Upvotes: 2