-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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 all commits
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,88 @@ 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, the exceptions will be overwritten by a response in json format:: | ||
|
||
# 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\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\JsonResponse; | ||
|
||
class ExceptionController extends BaseExceptionController | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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. |
||
{ | ||
$code = $exception->getStatusCode(); | ||
|
||
return new JsonResponse(array( | ||
'response_type' => 'error', | ||
'message' => 'An exception was thrown.', | ||
), $code); | ||
} | ||
} | ||
|
||
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 | ||
app.twig.exception_controller: | ||
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: app.twig.exception_controller: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>app.twig.exception_controller:showAction</twig:exception-controller> | ||
</twig:config> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('twig', array( | ||
'exception_controller' => 'app.twig.exception_controller: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