customize 403 error page depening on route in symfony 2.0

℡╲_俬逩灬. 提交于 2019-12-18 08:49:30

问题


i want to customize the error pages in Symfony 2.0

I know that this is done via overwriting the layouts in app/Resources/TwigBundle/views/Exception/* but I want to have different error pages for different routes.

I want one for backend and one for frontend.

How can I achieve this?


回答1:


What you'll need to do is not too difficult. Symfony allows you to explicitly specify which controller handles your exceptions. So, in your config.yml, you can specify the exception controller under your twig configuration:

since Symfony 2.2

twig:
   exception_controller:  my.twig.controller.exception:showAction

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

up to Symfony 2.1:

twig:
  exception_controller: AcmeDemoBundle\Controller\ExceptionController::showAction

Then you can create a custom showAction that displays a custom error page based on a route:

<?php
namespace AcmeDemoBundle\Controller;

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

class ExceptionController extends BaseExceptionController
{
    public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
    {
        if ($this->container->get('request')->get('_route') == "abcRoute") {
            $appTemplate = "backend";
        } else { 
            $appTemplate = "frontend";
        }

        $template = $this->container->get('kernel')->isDebug() ? 'exception' : 'error';
        $code = $exception->getStatusCode();

        return $this->container->get('templating')->renderResponse(
            'AcmeDemoBundle:Exception:' . $appTemplate . '_' . $template . '.html.twig',
            array(
                'status_code'    => $code,
                'status_text'    => Response::$statusTexts[$code],
                'exception'      => $exception,
                'logger'         => null,
                'currentContent' => '',
            )
        );
    }
}

Obviously you should probably customize the if statement where it tests the current route to fit your needs, but this should do it.

You might want to add code that defaults to the normal Twig error pages if you don't have a specific error template created. For more information, check out the code in

Symfony\Bundle\TwigBundle\Controller\ExceptionController

as well as

Symfony\Component\HttpKernel\EventListener\ExceptionListener



回答2:


i use arguments: ["@twig", "%kernel.debug%"] instead of arguments: [@twig, %kernel.debug%]



来源:https://stackoverflow.com/questions/14282690/customize-403-error-page-depening-on-route-in-symfony-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!