获取 Twig 模板中的当前 URL?
我四处寻找代码以获取 Twig 模板中的当前路径(而不是完整的 URL),即 我不需要 http://www.sitename.com/page
,我只需要 /page
。
I looked around for the code to get the current path in a Twig template (and not the full URL), i.e.
I don't want http://www.sitename.com/page
, I only need /page
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想将其读入视图变量:
app
全局视图变量包含各种有用的快捷方式,例如app.session
和app.security。 token.user
,引用您可能在控制器中使用的服务。If you want to read it into a view variable:
The
app
global view variable contains all sorts of useful shortcuts, such asapp.session
andapp.security.token.user
, that reference the services you might use in a controller.获取当前网址:Symfony 2.3、3、4、5中的
{{ app.request.uri }}
获取仅路径< /strong>:
{{ app.request.pathinfo }}
(不带参数)获取请求 uri:
{{ app.request.requesturi }}
> (带参数)Get current url:
{{ app.request.uri }}
in Symfony 2.3, 3, 4, 5Get path only:
{{ app.request.pathinfo }}
(without parameters)Get request uri:
{{ app.request.requesturi }}
(with parameters)在 symfony 2.1 中,您可以使用这个:
在 symfony 2.0 中,一种解决方案是为此编写一个 twig 扩展
并像这样使用
您不需要所有这些,除非您想向链接添加其他参数,例如在寻呼机中,或者您想要更改其中一个参数。
In symfony 2.1 you can use this:
In symfony 2.0, one solution is to write a twig extension for this
And use like this
You won't need all this unless you want to add additional parameters to your links, like in a pager, or you want to change one of the parameters.
您可以像这样获取 Twig 中的当前 URL:
You can get the current URL in Twig like this:
应该注意的是,如果您的 URL 中有其他查询参数,这些参数不属于配置的路由的一部分,则接受的答案不会将它们包含在当前 URL(路径)中。
为什么需要额外的参数?
例如,如果您有一个列表页面,其中包含可以按关键字过滤的记录,并且该页面具有分页功能,则“关键字”和“页面”的查询变量很可能不会出现在您的路由中。但在分页的前进和后退按钮中,您需要完整的当前 URL(包含关键字,以便仍过滤下一页)。并且需要修改页面变量。
如何合并额外的查询参数
这样您就可以获得当前路由,并合并额外的变量(在修改一个或多个额外变量之后)。请注意,您要将自己的变量合并到
app.request.query.all
中,然后将该数组合并到app.request.attributes.get('_route_params')
代码>.path()
方法要求您提供路由所需的所有参数,这就是您需要包含_route_params
的原因。{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge(app.request.query.all|merge({'page': 2 }))) }}
这确实很难看,但是如果您正在开发分页,则需要修改每个单独链接上的 page 变量,因此每次都必须包含整个内容。也许其他人有更好的解决方案。
It should be noted that if you have additional query parameters in your URL, which are not part of the configured route, the accepted answer will not include them in the current URL (path).
Why would you want extra parameters?
For example, if you have a list page with records that can be filtered by keyword and the page has pagination, most likely the query variables for "keyword" and "page" will not be in your route. But in your forward and back buttons for paging, you need the full current URL (that contains the keywords so the next page is still filtered). And you need to modify the page variable.
How to Merge In Extra Query Parameters
So you can get the current route, and merge in the extra variables (after modifying one or more of those extra variables). Note that you are merging in your own variables to the
app.request.query.all
, and then merging that array into theapp.request.attributes.get('_route_params')
. Thepath()
method requires that you provide all the required parameters of the route, which is why you need to include the_route_params
.{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge(app.request.query.all|merge({'page': 2 }))) }}
That's really ugly, but if you are developing pagination, you will need to modify the page variable on each separate link, so you have to include the whole thing each time. Perhaps others have a better solution.
如果您使用 Silex 2,您将无法再访问 Request 对象。
您可以通过这种方式访问当前请求属性。
并生成完整的当前 URL:
路径(app.request_stack.currentrequest.attributes.get('_route'),app.request_stack.currentrequest.attributes.get('_route_params'))
If you are using Silex 2 you can not access the Request object anymore.
You can access the current request attributes this way.
And to generate the full current URL :
path(app.request_stack.currentrequest.attributes.get('_route'), app.request_stack.currentrequest.attributes.get('_route_params'))
在 Shopware 6 Twig 文件中,您可以使用
Framework/Routing/RequestTransformer.php
中定义的sw-*
属性之一:In a Shopware 6 Twig file, you may use one of the
sw-*
attributes defined inFramework/Routing/RequestTransformer.php
: