我只想显示 /sitemap 页面,而不是错误页面 / 404。当然我不想要重定向,我仍然想要设置 404 HTTP 响应标头。
这可能吗?我只能看到如何在 Twig 中设置模板。
我绝对不想要重定向。
我只想显示 /sitemap 页面,而不是错误页面 / 404。当然我不想要重定向,我仍然想要设置 404 HTTP 响应标头。
这可能吗?我只能看到如何在 Twig 中设置模板。
我绝对不想要重定向。
As shown in the Symfony Cookbook you can override error pages in two ways:
If you only want to show the /sitemap
route on a 404 (HttpNotFoundException
) exception you could override the Twig exception template by creating a new template in app/Resources/TwigBundle/views/Exception/error404.html.twig
.
Another way that is not shown in the cookbook is using an event listener. When the kernel encounters an exception, a kernel.exception
event is dispatched. By default, this exception is caught by the exception listening provided by Twig. You can create your own event listener which listens for the kernel.exception
event and renders a page:
<?php
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($event->getException() instanceof NotFoundHttpException) {
$response = $this->templating->renderResponse(/* sitemap */);
$event->setResponse($response)
}
}
(I haven't tested this code, so you should try it yourself! And you have to inject the templating service into the event listener yourself, of course).
我有一个由几个 Symfony3 包组成的简单 Web 应用程序,所有这些包都扩展了 BaseController。我也有一个 AdminBundle。为了显示每个不存在的 url 的给定页面,我破解了(并简化了)原始的 ExceptionController.php(来自 TwigBundle):
<?php
namespace MyApp\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use MyApp\Controller\BaseController;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
class ExceptionController extends BaseController
{
/**
* Converts an Exception to a Response.
*
* @param Request $request The request
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
*
* @return Response
* @throws \InvalidArgumentException When the exception template does not exist
*/
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
$code = $exception->getStatusCode();
return $this->render('MyappAdminBundle:Admin:error.html.twig',
array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
'currentContent' => $currentContent,
)
);
}
/**
* @param int $startObLevel
*
* @return string
*/
protected function getAndCleanOutputBuffering($startObLevel)
{
if (ob_get_level() <= $startObLevel) {
return '';
}
Response::closeOutputBuffers($startObLevel + 1, true);
return ob_get_clean();
}
}
我根据自己的喜好定义我的错误模板。
我需要在 config.yml 中添加以下行
twig:
exception_controller: MyappAdminBundle:Exception:show
ps 我确信我的代码可以改进,但它工作正常。
最简单的方法是创建这个文件:
/app/Resources/TwigBundle/views/Exception/error404.html.twig
/templates/bundles/TwigBundle/Exception/error404.html.twig
...并将这一行复制到其中:
{{ render(controller('App\\Controller\\HomepageController::index')) }}
而已。这将显示带有 404 状态代码的主页。它只在prod
环境中工作(清除缓存!),因为在dev
和test
Symfony 中总是显示它的异常页面。
参考: