-
-
Couldn't load subscription status.
- Fork 5.3k
Custom ExceptionController using extended class #5757
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
ce994d8 af089f0 41f99d5 e7c658b de71bf1 d144a36 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 |
|---|---|---|
| | @@ -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:: | ||
| 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.
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 we explain very briefly why we want to override the method? Instead of: you can say something like: 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 updated the explanation, de71bf1. | ||
| | ||
| # src/AppBundle/Controller/ExceptionController.php | ||
| 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. we use | ||
| | ||
| 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 blank line should be removed | ||
| 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) | ||
| 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. The code of this method should be refactored a bit for brevity and readability. 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 was refactored for a simpler usage, e7c658b. | ||
| { | ||
| $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(); | ||
| 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. commented code 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. It was removed when i updated the example, e7c658b. | ||
| $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 | ||
| 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. We always try to avoid the first person perspective. Can you reword the sentence a bit? | ||
| 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: | ||
| 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. Should this service be called 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. Fixed, af089f0. | ||
| class: AppBundle\Controller\ExceptionController | ||
| arguments: [@twig, %kernel.debug%] | ||
| 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. Both arguments must be enclosed by (single) quotes to be valid YAML. Can you also add examples for the XML and PHP configuration formats? 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.
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. Actually, my last comment was wrong. We still need to register the controller as a service to be able to inject the needed dependencies. So we need examples for the other formats. | ||
| | ||
| To finally enable the custom exception controller, set the :ref:`twig.exception_controller | ||
| <config-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 | ||
| | ||
| <!-- app/config/config.xml --> | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns:twig="http://symfony.com/schema/dic/twig" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
| http://symfony.com/schema/dic/services/services-1.0.xsd | ||
| http://symfony.com/schema/dic/twig | ||
| http://symfony.com/schema/dic/twig/twig-1.0.xsd"> | ||
| | ||
| <twig:config> | ||
| <twig:exception-controller>appbundle.twig.controller.exception:showAction</twig:exception-controller> | ||
| </twig:config> | ||
| </container> | ||
| | ||
| .. 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 | ||
| | ||
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.
the class name should be enclosed by double backticks