From ce994d877e9ebec1ffd17b1dc9dc9a92710a577d Mon Sep 17 00:00:00 2001 From: Iago Melanias Date: Mon, 5 Oct 2015 20:10:51 -0300 Subject: [PATCH 1/6] Add more Custom ExceptionController instructions --- cookbook/controller/error_pages.rst | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index e2a5de8acc3..4c0a2cca9cb 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -214,11 +214,94 @@ will be passed two parameters: A :class:`\\Symfony\\Component\\HttpKernel\\Log\\DebugLoggerInterface` instance which may be ``null`` in some circumstances. +Extending from the Default ExceptionController Class +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Instead of creating a new exception controller from scratch you can, of course, also extend the default :class:`Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController`. In that case, you might want to override one or both of the ``showAction()`` and ``findTemplate()`` methods. The latter one locates the template to be used. +To create your own controller logic extending from the ExceptionController, simply +create a controller with the specified method you want to override. In this +example, we gonna override the showAction method:: + + # src/AppBundle/Controller/ExceptionController.php + + namespace AppBundle\Controller; + + use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\HttpKernel\Exception\FlattenException; + use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; + use Symfony\Component\HttpFoundation\Response; + + class ExceptionController extends \Symfony\Bundle\TwigBundle\Controller\ExceptionController + { + public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) + { + $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); + $showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC + + // $code = $exception->getStatusCode(); + $code = 500; // Route exceptions will throw status code 500 + + return new Response($this->twig->render( + (string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException), + array( + 'status_code' => $code, + 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', + 'exception' => $exception, + 'logger' => $logger, + 'currentContent' => $currentContent, + ) + )); + } + } + +To use the custom controller, we need to load the Twig service in the class, as +the original class does. To do it, add the service pointing to the controller +and set the arguments to load the specific needed services:: + + # app/config/services.yml + appbundle.twig.controller.exception: + class: AppBundle\Controller\ExceptionController + arguments: [@twig, %kernel.debug%] + +To finally enable the custom exception controller, set the :ref:`twig.exception_controller +` configuration option to point to the service controller. + + .. configuration-block:: + + .. code-block:: yaml + + # app/config/config.yml + twig: + exception_controller: appbundle.twig.controller.exception:showAction + + .. code-block:: xml + + + + + + + appbundle.twig.controller.exception:showAction + + + + .. code-block:: php + + // app/config/config.php + $container->loadFromExtension('twig', array( + 'exception_controller' => 'appbundle.twig.controller.exception:showAction', + // ... + )); + .. _use-kernel-exception-event: Working with the ``kernel.exception`` Event From af089f092de92c4f072723efa57ba58eeda0d5c6 Mon Sep 17 00:00:00 2001 From: Iago Melanias Date: Fri, 23 Oct 2015 22:31:47 -0200 Subject: [PATCH 2/6] Fix service name --- cookbook/controller/error_pages.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index 4c0a2cca9cb..807e356a8a4 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -262,7 +262,7 @@ the original class does. To do it, add the service pointing to the controller and set the arguments to load the specific needed services:: # app/config/services.yml - appbundle.twig.controller.exception: + app.twig.exception_controller: class: AppBundle\Controller\ExceptionController arguments: [@twig, %kernel.debug%] From 41f99d528b760cfcf49062f8dc5af5aec5f92c02 Mon Sep 17 00:00:00 2001 From: Iago Melanias Date: Fri, 23 Oct 2015 23:00:20 -0200 Subject: [PATCH 3/6] Fix service use --- cookbook/controller/error_pages.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index 807e356a8a4..a5a6aa8e5ec 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -228,7 +228,7 @@ example, we gonna override the showAction method:: # src/AppBundle/Controller/ExceptionController.php namespace AppBundle\Controller; - + use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; @@ -240,10 +240,10 @@ example, we gonna override the showAction method:: { $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); $showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC - + // $code = $exception->getStatusCode(); $code = 500; // Route exceptions will throw status code 500 - + return new Response($this->twig->render( (string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException), array( @@ -275,7 +275,7 @@ To finally enable the custom exception controller, set the :ref:`twig.exception_ # app/config/config.yml twig: - exception_controller: appbundle.twig.controller.exception:showAction + exception_controller: app.twig.exception_controller:showAction .. code-block:: xml @@ -290,7 +290,7 @@ To finally enable the custom exception controller, set the :ref:`twig.exception_ http://symfony.com/schema/dic/twig/twig-1.0.xsd"> - appbundle.twig.controller.exception:showAction + app.twig.exception_controller:showAction @@ -298,7 +298,7 @@ To finally enable the custom exception controller, set the :ref:`twig.exception_ // app/config/config.php $container->loadFromExtension('twig', array( - 'exception_controller' => 'appbundle.twig.controller.exception:showAction', + 'exception_controller' => 'app.twig.exception_controller:showAction', // ... )); From e7c658b204dc47fade9077edb06c46639a187c85 Mon Sep 17 00:00:00 2001 From: Iago Melanias Date: Fri, 23 Oct 2015 23:02:40 -0200 Subject: [PATCH 4/6] Use simpler example --- cookbook/controller/error_pages.rst | 32 ++++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index a5a6aa8e5ec..6be894bebaf 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -229,31 +229,25 @@ example, we gonna override the showAction method:: namespace AppBundle\Controller; + use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; - use Symfony\Component\HttpFoundation\Response; - - class ExceptionController extends \Symfony\Bundle\TwigBundle\Controller\ExceptionController + use Symfony\Component\HttpFoundation\JsonResponse; + + class ExceptionController extends BaseExceptionController { + /** + * {@inheritdoc} + */ public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) { - $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); - $showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC - - // $code = $exception->getStatusCode(); - $code = 500; // Route exceptions will throw status code 500 - - return new Response($this->twig->render( - (string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException), - array( - 'status_code' => $code, - 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', - 'exception' => $exception, - 'logger' => $logger, - 'currentContent' => $currentContent, - ) - )); + $code = $exception->getStatusCode(); + + return new JsonResponse(array( + 'response_type' => 'error', + 'message' => 'An exception was thrown.' + ), $code); } } From de71bf16f2def612a7fba47f8933214df351949a Mon Sep 17 00:00:00 2001 From: Iago Melanias Date: Fri, 23 Oct 2015 23:15:27 -0200 Subject: [PATCH 5/6] Fix example explanation --- cookbook/controller/error_pages.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index 6be894bebaf..a8ad9ec5401 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -223,7 +223,7 @@ In that case, you might want to override one or both of the ``showAction()`` and To create your own controller logic extending from the ExceptionController, simply create a controller with the specified method you want to override. In this -example, we gonna override the showAction method:: +example, the exceptions will be overwritten by a response in json format:: # src/AppBundle/Controller/ExceptionController.php From d144a36c3f459a46bfe5e24e9f6bc0e81bf67204 Mon Sep 17 00:00:00 2001 From: Iago Melanias Date: Fri, 23 Oct 2015 23:15:34 -0200 Subject: [PATCH 6/6] Fix code style --- cookbook/controller/error_pages.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/controller/error_pages.rst b/cookbook/controller/error_pages.rst index a8ad9ec5401..7669b67ffc0 100644 --- a/cookbook/controller/error_pages.rst +++ b/cookbook/controller/error_pages.rst @@ -246,7 +246,7 @@ example, the exceptions will be overwritten by a response in json format:: return new JsonResponse(array( 'response_type' => 'error', - 'message' => 'An exception was thrown.' + 'message' => 'An exception was thrown.', ), $code); } }