Skip to content

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

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions cookbook/controller/error_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use // for PHP comments


Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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%]
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

@xabbuh xabbuh May 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I think we should omit the service definition and instead use AppBundle:Exception:show in the examples below instead.

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down