Skip to main content

Symfony

Symfony Debug to Error Handler Component

·260 words·2 mins
Since symfony 4.4 the symfony/debug component is deprecated. The “\Symfony\Component\Debug\ErrorHandler” class is deprecated since Symfony 4.4, use “\Symfony\Component\ErrorHandler\ErrorHandler” Running unit tests on Mascot, I noticed lots of warnings. But it was not easy as changing Symfony\Component\Debug\ExceptionHandler to Symfony\Component\ErrorHandler\ErrorHandler. Assume we have something as below 37 38 39 40 41 42 43 44 <?php $handler = new \Symfony\Component\Debug\ExceptionHandler($debug); $exception = $event->getThrowable(); if (!$exception instanceof FlattenException) { $exception = FlattenException::create($exception); } $response = Response::create($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders())->setCharset(ini_get('default_charset')); $event->setResponse($response); The Symfony\Component\ErrorHandler\ErrorHandler has no getHtml method. From the symfony docs, if you are in development mode we can use below code.

Custom Events in a Symfony2 Bundle

·301 words·2 mins
In this tutorial we will create a custom event for symfony2 bundle. Assuming you have downloaded the symfony-standard distribution to play. Create HktEventBundle via sensio generator bundle. 1 2 <?php php app/console generate:bundle --namespace=Hkt/EventBundle --dir src --no-interaction Create the event class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?php // src/Hkt/EventBundle/Event/PageViewed.php namespace Hkt\EventBundle\Event; use Symfony\Component\EventDispatcher\Event; class PageViewed extends Event { protected $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } Add as many methods/properties which are needed from the listener. Instead of creating an event class we can make use of generic event also.