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