根据 symfony 2.0 中的路由自定义 403 错误页面 [英] customize 403 error page depening on route in symfony 2.0

查看:32
本文介绍了根据 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.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

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

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: AcmeDemoBundleControllerExceptionController
        arguments: [@twig, %kernel.debug%]

直到 Symfony 2.1:

up to Symfony 2.1:

twig:
  exception_controller: AcmeDemoBundleControllerExceptionController::showAction

然后你可以创建一个自定义的 showAction 来显示基于路由的自定义错误页面:

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

<?php
namespace AcmeDemoBundleController;

use SymfonyComponentHttpKernelExceptionFlattenException;
use SymfonyComponentHttpKernelLogDebugLoggerInterface;
use SymfonyComponentHttpFoundationResponse;
use SymfonyBundleTwigBundleControllerExceptionController 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

SymfonyBundleTwigBundleControllerExceptionController

还有

SymfonyComponentHttpKernelEventListenerExceptionListener

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

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