2
A
回答
6
我有同样的问题,解决方案很简单。您必须修改参数twig.exception_listener.contoller
以将错误页面的呈现重定向到您自己的控制器,这可能会扩展原始Twig异常控制器。
例(YourBundle/Resources/config/services.xml
):
<parameter key="twig.exception_listener.controller">YourBundle\Controller\ExceptionController::showAction</parameter>
然后,你必须使用方法showAction
创建自己的ExceptionController,检查环境,做你想做的事或通过请求parent::showAction()
什么。
namespace YourBundle\Controller;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Response;
class ExceptionController extends BaseExceptionController {
public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html') {
$kernel = $this->container->get('kernel');
if ($kernel->getEnvironment() == 'prod') {
$request = $this->container->get('request');
$request->setRequestFormat($format);
$templating = $this->container->get('templating');
$code = $exception->getStatusCode();
$template = new TemplateReference('YourBundle', 'Exception', 'errorpage', $format, 'twig');
if ($templating->exists($template)) {
$response = $templating->renderResponse($template, array(
'status_code' => $code,
'message_code' => 'error_' . $code,
'status_text' => Response::$statusTexts[$code],
'requested_url' => $request->getUri(),
));
$response->setStatusCode($code);
$response->headers->replace($exception->getHeaders());
return $response;
}
}
return parent::showAction($exception, $logger, $format);
}
}
要小心errorpage.html.twig中的错误,因为在小枝处理中的例外情况不会像平常那样处理。
0
如果你不希望覆盖异常控制器:在
\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\Resources\views\
到
\app\Resources\TwigBundle\views
然后
你可以先复制整个文件夹(或者具体布局文件)定制每个布局文件中的视图以匹配您的设计。
然后在布局文件,自定义消息为每个环境如下
{% if app.environment == 'prod' %}
// message for prod
{% else %}
// message for dev
{% endif %}
相关问题
- 1. 自定义错误页面不适用于Weld&Tomcat7
- 2. 发布环境特定appsettings.ENV.json只适用于各自的环境
- 3. Symfony2.1的自定义错误页面在产品环境中不起作用
- 4. Google云端平台,Golang灵活环境HTTPS仅适用于自定义域名
- 5. WCF无法仅在PROD环境中创建自定义端点行为
- 6. .net defaultRedirect到特定于语言环境的错误页面
- 7. 使用Docker运行Hadoop(适用于DEV和PROD环境)
- 8. Wicket setPageExpiredErrorPage仅适用于特定页面?
- 9. Log4j2自定义插件不适用于OSGi环境
- 10. IIS自定义错误未显示自定义错误页面
- 11. 自定义错误页面使用global.asax
- 12. 服务容器别名仅适用于一定的环境
- 13. Silverlight中的自定义错误页面
- 14. joomla中的自定义错误页面?
- 15. CakePHP Plugin的自定义错误页面
- 16. JBoss中的自定义错误页面
- 17. 闪亮的自定义错误页面
- 18. SharePoint 2007的自定义页面错误
- 19. asp.net中的自定义错误页面
- 20. 分页不适用于自定义页面索引
- 21. Rails,开发环境和错误页面
- 22. 自定义错误适用于错误,但在404上失败
- 23. 404错误自定义页面适用于.cfm扩展名,但不适用于.html?
- 24. asp.net中的自定义错误页面无尽循环
- 25. 仅适用于一个页面的HTTPS
- 26. ASP.NET MVC应用程序的自定义错误页,共享托管环境
- 27. symfony2自定义错误页面
- 28. nGinx自定义错误页面
- 29. SparkJava自定义错误页面
- 30. Apache2 GeoIP/mod_geoip2自定义错误页面
你需要的是INT食谱(DEV异常页面仍然是相同的,它不仅影响PROD环境):HTTP:/ /symfony.com/doc/current/cookbook/controller/error_pages.html – AlterPHP 2012-02-28 04:56:44