-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added "How to create an event subscriber" #4538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bb9818a
a6348b2
900a70e
be3143c
5084c5a
bec7cf9
626ddca
2bca2c5
5d02ef7
0ed66ec
ab79a9c
9aba4ad
00b16ae
f8b769c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
.. index:: | ||
single: Events; Create listener | ||
single: Events; Create listener, Create subscriber | ||
|
||
How to Create an Event Listener | ||
=============================== | ||
How to Create Event Listeners and Subscribers | ||
============================================= | ||
|
||
Symfony has various events and hooks that can be used to trigger custom | ||
behavior in your application. Those events are thrown by the HttpKernel | ||
component and can be viewed in the :class:`Symfony\\Component\\HttpKernel\\KernelEvents` class. | ||
|
||
To hook into an event and add your own custom logic, you have to create | ||
a service that will act as an event listener on that event. In this entry, | ||
you will create a service that will act as an Exception Listener, allowing | ||
a service that will act as an event listener on that event. You can do that in two different ways, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. service that will listen to that event. |
||
creating an Event Listener or an Event Subscriber instead. In this entry, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you use "Event Listener" and "Event Subscriber", but in the rest of the article it's "event listener" and "event subscriber". Is there any reason for the capitalization of letters in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, there's no reason to do that, I'm going to fix it, thanks. |
||
you will see the two ways of creating a service that will act as an Exception Listener, allowing | ||
you to modify how exceptions are shown by your application. The ``KernelEvents::EXCEPTION`` | ||
event is just one of the core kernel events:: | ||
event is just one of the core kernel events. | ||
|
||
Creating an Event Listener | ||
-------------------------- | ||
|
||
The most common way to listen to an event is to register an event listener: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has to be a double colon ( |
||
|
||
// src/Acme/DemoBundle/EventListener/AcmeExceptionListener.php | ||
namespace Acme\DemoBundle\EventListener; | ||
|
@@ -97,6 +103,98 @@ using a special "tag": | |
in the order of their priority (highest to lowest). This is useful when | ||
you need to guarantee that one listener is executed before another. | ||
|
||
Creating an Event Subscriber | ||
---------------------------- | ||
|
||
Another way to listen to events is via an event subscriber. An event subscriber can define one or various method | ||
that listen to one or various events, and can stablish a priority for each method. The following example shows | ||
a subscriber that subscribes various methods to the kernel.exception event: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a line break after the first word that crosses the 72th column? |
||
|
||
// src/Acme/DemoBundle/EventListener/AcmeExceptionSubscriber.php | ||
namespace Acme\DemoBundle\EventSubscriber; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about using the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'll change it |
||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; | ||
|
||
class AcmeExceptionSubscriber implements EventSubscriberInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove the vendor from here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't you keep the EventSubscriberInterface vendor for clarity? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, "Acme". Just "ExceptionSubscriber" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry but I'm afraid that I don't understand you. Could you please give me an example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - class AcmeExceptionSubscriber implements EventSubscriberInterface
+ class ExceptionSubscriber implements EventSubscriberInterface There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the example, I feel quite stupid right now... |
||
{ | ||
public static function getSubscribedEvents() | ||
{ | ||
// Return the events it is subscribed to, the methods that listen each event and the | ||
// priority of each method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we could also add a minor note about priority: ... priority of each method (the higher, the more priority) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't do that because there is already a note about priority in the event listener above, and I thought that it's obvious, but if you think thats it's necessary I can do it. |
||
return array( | ||
'kernel.exception' => array( | ||
array('onKernelExceptionPre', 10), | ||
array('onKernelExceptionMid', 5), | ||
array('onKernelExceptionPost', 0), | ||
) | ||
); | ||
} | ||
|
||
public function onKernelExceptionPre(GetResponseForExceptionEvent $event) | ||
{ | ||
$exception = $event->getException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why we show the code of this method and not the other ones. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because I only wanted to show how to define an event subscriber, and so, show that in an event subscriber we can define various methods that listen to the same event, but I thought that the code of the method wasn't relevant for the example. I added the code of one method to show the similarity between the method in the event listener and the same functionality in the event subscriber one. Therefore, I can remove the code of the method or add some sample code to the other ones. Which do you think is the best option? |
||
$message = sprintf( | ||
'My Error says: %s with code: %s', | ||
$exception->getMessage(), | ||
$exception->getCode() | ||
); | ||
|
||
$response = new Response(); | ||
$response->setContent($message); | ||
|
||
if ($exception instanceof HttpExceptionInterface) { | ||
$response->setStatusCode($exception->getStatusCode()); | ||
$response->headers->replace($exception->getHeaders()); | ||
} else { | ||
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
$event->setResponse($response); | ||
} | ||
|
||
public function onKernerlExceptionMid(GetResponseForExceptionEvent $event) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method name sounds a bit weird to me. |
||
{ | ||
// ... | ||
} | ||
|
||
public function onKernerlExceptionPost(GetResponseForExceptionEvent $event) | ||
{ | ||
// ... | ||
} | ||
} | ||
|
||
Now, you just need to register the class as a service and notify Symfony that it | ||
is an event subscriber: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
services: | ||
kernel.listener.your_subscriber_name: | ||
class: Acme\DemoBundle\EventSubscriber\AcmeExceptionSubscriber | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You used an AppBundle and an |
||
tags: | ||
- { name: kernel.event_subscriber } | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<service id="kernel.listener.your_subscriber_name" class="Acme\DemoBundle\EventSubscriber\AcmeExceptionSubscriber"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please change this to: <?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services">
<services>
<service id="acme_exception_subscriber"
class="Acme\DemoBundle\EventSubscriber\AcmeExceptionSubscriber">
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container> |
||
<tag name="kernel.event_subscriber" /> | ||
</service> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container | ||
->register('kernel.listener.your_subscriber_name', 'Acme\DemoBundle\EventSubscriber\AcmeExceptionSubscriber') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please line wrap this: $container
->register(
'acme_exception_subscriber',
'Acme\DemoBundle\EventSubscriber\AcmeExceptionSubscriber'
)
->addTag('kernel.event_subscriber')
; |
||
->addTag('kernel.event_subscriber')) | ||
; | ||
|
||
Request Events, Checking Types | ||
------------------------------ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? I think you will have so split this into two indexes:
.. index:: single: Events; Create listener single: Events; Create subscriber