在symfony 2.0中自定义403错误页面 [英] customize 403 error page depening on route in symfony 2.0

查看:108
本文介绍了在symfony 2.0中自定义403错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Symfony 2.0中自定义错误页面

i want to customize the error pages in Symfony 2.0

我知道这是通过覆盖app/Resources/TwigBundle/views/Exception/*中的布局来完成的,但是我想为不同的路线设置不同的错误页面.

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.

我该如何实现?

推荐答案

您需要做的并不是很困难. Symfony允许您明确指定哪个控制器处理您的异常.因此,在config.yml中,您可以在树枝配置下指定异常控制器:

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:

自Symfony 2.2起

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%]

Symfony 2.1之前的版本:

up to Symfony 2.1:

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

然后,您可以创建一个自定义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' => '',
            )
        );
    }
}

很显然,您可能应该自定义if语句,以在其中测试当前路由以适合您的需求,但这应该可以做到.

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

如果您没有创建特定的错误模板,则可能需要添加默认为正常Twig错误页面的代码.有关更多信息,请查看

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

以及

Symfony\Component\HttpKernel\EventListener\ExceptionListener

这篇关于在symfony 2.0中自定义403错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆