If a thing happens in common it can be said to be an Occasion. We use situations in order to accomplish some responsibilities if an celebration happens, these responsibilities can be realized by applying the followings:
- Occasion Subscriber
- Party Listeners
Celebration Subscriber vs Event Listeners
They equally provide the same objective but have various implementations. Both equally trigger some capabilities in the particular time of processing facts by Symfony.
We can declare several features inside an party subscriber and set the priorities of the functions in the buy in which each individual purpose will be identified as but it cannot be reached in occasion listeners, so internally Symfony also utilizes function subscriber in excess of celebration listener.
It is declared as follows:
// src/EventSubscriber/ExceptionSubscriber.php namespace AppEventSubscriber use SymfonyComponentEventDispatcherEventSubscriberInterface use SymfonyComponentHttpKernelEventExceptionEvent use SymfonyComponentHttpKernelKernelEvents class ExceptionSubscriber implements EventSubscriberInterface general public static perform getSubscribedEvents() return [ KernelEvents::EXCEPTION => [ ['handleException', 1], ['notifyAdminUser', 2], ], ] community functionality handleException(ExceptionEvent $function) // ... general public functionality notifyAdminUser(ExceptionEvent $function) // ...
This party subscriber handles the kernel exceptions like when we do not determine routes in our venture then this subscriber will be referred to as. Also, we require to register this subscriber in the config/services.yml file.
AppEventSubscriberExceptionSubscriber: tags: - doctrine.party_subscriber, link: default
The subscriber function with the higher number established as precedence will be identified as initial and the subscriber operate with the decrease variety established as priority will be named past. Right here the purpose notifyAdminUser will be referred to as and then the perform handleException will be identified as.
As described above the function listener is employed for the identical cause. It is declared as follows:
// src/EventListener/ExceptionListener.php namespace AppEventListener use SymfonyComponentHttpFoundationResponse use SymfonyComponentHttpKernelEventExceptionEvent use SymfonyComponentHttpKernelExceptionHttpExceptionInterface class ExceptionListener public perform onKernelException(ExceptionEvent $occasion) $exception = $party->getThrowable() $message = sprintf( 'Getting pursuing error: %s with code: %s', $exception->getMessage(), $exception->getCode() ) // Customise your reaction item to show the exception information $reaction = new Response() $response->setContent($message) // HttpExceptionInterface is a specific sort of exception that // holds standing code and header details if ($exception instanceof HttpExceptionInterface) $reaction->setStatusCode($exception->getStatusCode()) $reaction->headers->replace($exception->getHeaders()) else $response->setStatusCode(Response::HTTP_Internal_SERVER_Mistake) // sends the modified response item to the occasion $occasion->setResponse($response)
This listener will be referred to as at the time a kernel exception takes place like calling a route that does not exist. In this scenario, this listener will be termed to cope with the exception. Also, we have to have to register this listener in the config/products and services.yml file.
services: AppEventListenerExceptionListener: tags: - identify: kernel.occasion_listener, occasion: kernel.exception
Many thanks for studying me. I hope this blog site would assist you with a superior comprehending of Symfony subscribers and listeners. You should share your critiques on this, which will help me to produce much more.
Until finally up coming time. 👋