Creating a 404 error page in symfony (creating a controller)
Come back
9

Creating a 404 error page in symfony (creating a controller)

I have already created an "AppBundle" bundle and there is a controller folder in which there will be controllers. We register the controller in services config.yml, write the settings and the path to the controller:

# This should be written in symfony 2.2 !!! and higher
twig:
    exception_controller: my.twig.controller.exception:showAction
# This should be written in symfony 2.1 version !!!
twig:
    exception_controller: AppBundle\Controller\ExceptionController:showAction


services:
    my.twig.controller.exception:
        class: AppBundle\Controller\ExceptionController
        arguments: ['@twig', '%kernel.debug%', '@service_container']

Where class we write the path to our class, the controller, which we will then create in the bundle: 

- class: AppBundle\Controller\ExceptionController

And we specify the function in the controller ExceptionController, which will work on page transition: :showAction

The controller itself is referenced in the specified twig parameters by the name of the service:

- my.twig.controller.exception

In the arguments, I wrote the input parameters to the construction of the ExceptionController class:

- arguments: ['@twig', '%kernel.debug%', '@service_container']

After we have registered the paths and settings for the controller in yml, we proceed to creating the ExceptionController itself along the path AppBundle\Controller :

namespace FrontBundle\Controller;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;

class ExceptionController extends BaseExceptionController
{
    private $container;
    
    public function __construct(Environment $twig, $debug, $container)
    {
        parent::__construct($twig, $debug);

        $this->container = $container;
    }
    
    public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
    {
        $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
        $showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional 
        $code = $exception->getStatusCode();

        if($code != '404' && $code == ''){
            $code = '404';
        }

        $responce = new Response($this->twig->render(
            '@Front/Exception/error'.$code.'.html.twig',
            [
                'status_code' => $code,
                'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
                'exception' => $exception,
                'logger' => $logger,
                'currentContent' => $currentContent,
            ]
        ), 200, ['Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html']);

        return $responce;
    }
}

To display a 404 error, specify the path to the template @Front/Exception/error'.$code.'.html.twig and pass the variables there

That's all, now when you switch to not not correct page this controller fires

created:
05.09.2020
updated:
22.04.2023
author:
admin
Tags
yandex metrika