当我覆盖 404 错误页面时 app.request.attributes.get('_route') 为空 [英] app.request.attributes.get('_route') is empty when I override 404 error page

查看:23
本文介绍了当我覆盖 404 错误页面时 app.request.attributes.get('_route') 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义 404 错误页面,所以我做到了:

I'm trying to customize 404 error page so I did:

1) 创建 app/Resources/TwigBundle/views/Exception/error404.html.twig

1) Create app/Resources/TwigBundle/views/Exception/error404.html.twig

2) 我在layout.html.twig"中将 is_granted() 更改为 {% if app.user 和 is_granted('...') %},由error404.html.twig"使用

2) I changed is_granted() for {% if app.user and is_granted('...') %} in "layout.html.twig", used by "error404.html.twig"

3) php app/console cache:clear --env=prod --no-debug

3) php app/console cache:clear --env=prod --no-debug

4) 在 prod 环境中访问 http://localhost/app.php 并导致 404 错误,得到一个空白页面而不是我的自定义错误页面.

4) Access in prod enviroment to http:/localhost/app.php and cause 404 error, getting a blank page instead of my custom error page.

我检查了我的prod.log"文件,我看到了 NotFoundHttpException 和这个 Twig 错误:

I checked my "prod.log" file and I saw a NotFoundHttpException and this Twig error:

request.ERROR:处理异常时抛出异常(Twig_Error_Runtime: 渲染过程中抛出异常模板(无法为命名路由生成 URL"作为这样的路线不存在.")在第 33 行的::layout.html.twig"中.)[] []

request.ERROR: Exception thrown when handling an exception (Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "" as such route does not exist.") in "::layout.html.twig" at line 33.) [] []

看来 path(app.request.attributes.get('_route')) 返回一个空值.

It appears that path(app.request.attributes.get('_route')) returns an empty value.

-> 如何获取当前路线并使错误页面正常工作?<-

-> How can I get current route and make error page work?<-

谢谢

推荐答案

正如 Maerlyn 在他的 评论,如果您在 404 页面上,很可能没有匹配的路由.

As Maerlyn says in his comment, it's likely that, if you're on a 404 page, there is no matching route.

但是,您可以从请求对象中检索有用的信息,这可能对您有所帮助:

However, you can retrieve useful information from the request object which may help you here:

{{ app.request.uri }}

将返回用户试图访问的页面的完整 URL.还有

will return you the full URL of the page the user was trying to access. And

{{ app.request.pathInfo }}

将返回路径(以斜杠开头).

will return the path (starting with a slash).

您可以通过查看 /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Request.php 来了解有关请求对象上可用方法的更多信息.

You can find out more about the methods available on the request object by looking at /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Request.php.

这篇关于当我覆盖 404 错误页面时 app.request.attributes.get('_route') 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆