Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce994d8

Browse files
committed
Add more Custom ExceptionController instructions
1 parent 1462b8a commit ce994d8

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

cookbook/controller/error_pages.rst

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,94 @@ will be passed two parameters:
214214
A :class:`\\Symfony\\Component\\HttpKernel\\Log\\DebugLoggerInterface`
215215
instance which may be ``null`` in some circumstances.
216216

217+
Extending from the Default ExceptionController Class
218+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217219
Instead of creating a new exception controller from scratch you can, of course,
218220
also extend the default :class:`Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController`.
219221
In that case, you might want to override one or both of the ``showAction()`` and
220222
``findTemplate()`` methods. The latter one locates the template to be used.
221223

224+
To create your own controller logic extending from the ExceptionController, simply
225+
create a controller with the specified method you want to override. In this
226+
example, we gonna override the showAction method::
227+
228+
# src/AppBundle/Controller/ExceptionController.php
229+
230+
namespace AppBundle\Controller;
231+
232+
use Symfony\Component\HttpFoundation\Request;
233+
use Symfony\Component\HttpKernel\Exception\FlattenException;
234+
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
235+
use Symfony\Component\HttpFoundation\Response;
236+
237+
class ExceptionController extends \Symfony\Bundle\TwigBundle\Controller\ExceptionController
238+
{
239+
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
240+
{
241+
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
242+
$showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC
243+
244+
// $code = $exception->getStatusCode();
245+
$code = 500; // Route exceptions will throw status code 500
246+
247+
return new Response($this->twig->render(
248+
(string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
249+
array(
250+
'status_code' => $code,
251+
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
252+
'exception' => $exception,
253+
'logger' => $logger,
254+
'currentContent' => $currentContent,
255+
)
256+
));
257+
}
258+
}
259+
260+
To use the custom controller, we need to load the Twig service in the class, as
261+
the original class does. To do it, add the service pointing to the controller
262+
and set the arguments to load the specific needed services::
263+
264+
# app/config/services.yml
265+
appbundle.twig.controller.exception:
266+
class: AppBundle\Controller\ExceptionController
267+
arguments: [@twig, %kernel.debug%]
268+
269+
To finally enable the custom exception controller, set the :ref:`twig.exception_controller
270+
<config-twig-exception-controller>` configuration option to point to the service controller.
271+
272+
.. configuration-block::
273+
274+
.. code-block:: yaml
275+
276+
# app/config/config.yml
277+
twig:
278+
exception_controller: appbundle.twig.controller.exception:showAction
279+
280+
.. code-block:: xml
281+
282+
<!-- app/config/config.xml -->
283+
<?xml version="1.0" encoding="UTF-8" ?>
284+
<container xmlns="http://symfony.com/schema/dic/services"
285+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
286+
xmlns:twig="http://symfony.com/schema/dic/twig"
287+
xsi:schemaLocation="http://symfony.com/schema/dic/services
288+
http://symfony.com/schema/dic/services/services-1.0.xsd
289+
http://symfony.com/schema/dic/twig
290+
http://symfony.com/schema/dic/twig/twig-1.0.xsd">
291+
292+
<twig:config>
293+
<twig:exception-controller>appbundle.twig.controller.exception:showAction</twig:exception-controller>
294+
</twig:config>
295+
</container>
296+
297+
.. code-block:: php
298+
299+
// app/config/config.php
300+
$container->loadFromExtension('twig', array(
301+
'exception_controller' => 'appbundle.twig.controller.exception:showAction',
302+
// ...
303+
));
304+
222305
.. _use-kernel-exception-event:
223306

224307
Working with the ``kernel.exception`` Event

0 commit comments

Comments
 (0)