Show a specific route instead of error page (404) - Symfony2

前端 未结 3 1518
面向向阳花
面向向阳花 2020-12-15 10:08

Instead of an error page / 404 I want to just show the /sitemap page. Of course I don\'t want a redirect and I still want the 404 HTTP response header to be set.

Is

3条回答
  •  青春惊慌失措
    2020-12-15 10:44

    The easiest way is to create this file:

    • Symfony 3: /app/Resources/TwigBundle/views/Exception/error404.html.twig
    • Symfony 4: /templates/bundles/TwigBundle/Exception/error404.html.twig

    ... and copy this line into it:

    {{ render(controller('App\\Controller\\HomepageController::index')) }}
    

    That's it. This will show the home page with a 404 status code. It only works in prod environment (clear the cache!), since in dev and test Symfony always shows its exception page.

    References:

    • https://symfony.com/doc/current/controller/error_pages.html#overriding-the-default-error-templates
    • https://symfony.com/doc/current/templating/embedding_controllers.html
    • https://symfony.com/doc/current/controller/error_pages.html#testing-error-pages-during-development

提交回复
热议问题