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. 发布环境特定appsettings.ENV.json只适用于各自的环境
- 2. WCF无法仅在PROD环境中创建自定义端点行为
- 3. .net defaultRedirect到特定于语言环境的错误页面
- 4. 使用Docker运行Hadoop(适用于DEV和PROD环境)
- 5. Log4j2自定义插件不适用于OSGi环境
- 6. SharePoint 2007的自定义页面错误
- 7. JBoss中的自定义错误页面
- 8. CakePHP Plugin的自定义错误页面
- 9. joomla中的自定义错误页面?
- 10. Rails,开发环境和错误页面
- 11. asp.net中的自定义错误页面无尽循环
- 12. ASP.NET MVC应用程序的自定义错误页,共享托管环境
- 13. ASP.net自定义错误页面
- 14. Apache2.4.10自定义错误页面
- 15. 自定义错误页面InvalidArgumentException Symfony2
- 16. Index.php as自定义错误页面
- 17. 瓶颈JSON自定义错误页面
- 18. 自定义错误页面不显示
- 19. 更改自定义404错误页面
- 20. nGinx自定义错误页面
- 21. 自定义错误页面404
- 22. 没有自定义404错误页面
- 23. MVC5自定义错误页面
- 24. Kohana 2自定义404错误页面
- 25. sharepoint 2013自定义错误页面
- 26. Symfony PHP Template自定义错误页面
- 27. Appcache和自定义错误页面
- 28. ASP.NET自定义错误页面
- 29. apache tomcat 503自定义错误页面
- 30. 自定义错误页面桶S3
你需要的是INT食谱(DEV异常页面仍然是相同的,它不仅影响PROD环境):HTTP:/ /symfony.com/doc/current/cookbook/controller/error_pages.html – AlterPHP 2012-02-28 04:56:44