Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

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

Choose a reason for hiding this comment

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

we gonna override is too casual for the Symfony doc 😄

Copy link
Member

Choose a reason for hiding this comment

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

we gonna override the showAction method:: 

you can say something like:

the application overrides the ``showAction()`` method to do THIS and THAT:: 
Copy link
Author

Choose a reason for hiding this comment

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

I updated the explanation, de71bf1.

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\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)
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.

{
$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();
Copy link
Member

Choose a reason for hiding this comment

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

commented code here

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 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
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
appbundle.twig.controller.exception:
Copy link
Member

Choose a reason for hiding this comment

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

Should this service be called app.twig.exception_controller instead?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed, af089f0.

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