简体   繁体   中英

Custom Exception Controller in Symfony 4

I am building a custom exception controller in Symfony 4 to overwrite the ExceptionController class included in the Twig bundle.

I am doing this as per the Symfony documentation for customizing error pages .

# config/packages/twig.yaml
twig:
    exception_controller: App\Controller\Error::handleException

The reason I am using a custom exception controller is because I need to pass some additional variable to the template that are given by a custom BaseController class.

The Symfony docs mention the following about using a custom controller:

The ExceptionListener class used by the TwigBundle as a listener of the kernel.exception event creates the request that will be dispatched to your controller. In addition, your controller will be passed two parameters:

 exception A FlattenException instance created from the exception being handled. logger A DebugLoggerInterface instance which may be null in some circumstances.

I need the FlattenException service to determine the error code but its not clear from the docs how these parameters are passed to the custom exception controller.

Here is my custom exception controller code:

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Debug\Exception\FlattenException;

class Error extends BaseController {

    protected $debug, // this is passed as a parameter from services.yaml
              $code;  // 404, 500, etc.

    public function __construct(BaseController $Base, bool $debug) {

        $this->debug = $debug;

        $this->data = $Base->data;

        // I'm instantiating this class explicitly here, but have tried autowiring and other variations that all give an error.
        $exception = new FlattenException();

        $this->code = $exception->getStatusCode(); // empty

    }

    public function handleException(){

        $template = 'error' . $this->code . '.html.twig';
        return new Response($this->renderView($template, $this->data));
    }
} 

From the documentation page you are linking, at the very beginning of the chapter Overriding the default template the documentation actually cross link you to the class \\Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController , and this shows you how to use it.

So as per Symfony's own ExceptionController , the FlattenException is actually an argument of the action showAction :

<?php

namespace App\Controller;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;    
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;

class Error extends BaseController {

    protected $debug; // this is passed as a parameter from services.yaml
    protected $code;  // 404, 500, etc.
    protected $data;

    public function __construct(BaseController $base, bool $debug) {

        $this->debug = $debug;

        $this->data = $base->data;

    }

    public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) {
        // dd($exception); // uncomment me to see the exception

        $template = 'error' . $exception-> getStatusCode() . '.html.twig';
        return new Response($this->renderView($template, $this->data));
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM