php Symfony2 - 在 TWIG 模板中获取当前 URL 或路由?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28913347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:08:20  来源:igfitidea点击:

Symfony2 - Get the current URL or route in TWIG template?

phpsymfonytwig

提问by Wizard

My route is

我的路线是

admin:
      path:     /admin/
      defaults: { _controller: CatalogWebBundle:Admin:admin }

How I can get route name in PHP template ?

如何在 PHP 模板中获取路由名称?

回答by Tom Tom

To get the current URL

获取当前 URL

$request->getRequestUri();or app.request.uri

$request->getRequestUri();或者 app.request.uri

As for the route itself, the best practice is to inject it as parameter in your controller, see the doc here. You could use $request->attributes->get('_route')or app.request.attributes.get('_route')but it is not as reliable, for example it won't work with forwardsas you are forwarding to a controller, not to a path. And it is really only meant for debugging purposes according to Fabien (@fabpot), the creator, so I would not rely on it for future upgrades' sake.

至于路由本身,最佳做法是将其作为参数注入控制器,请参阅此处的文档。您可以使用$request->attributes->get('_route')orapp.request.attributes.get('_route')但它不那么可靠,例如它不会像转发到控制器而不是路径那样与转发一起使用。根据创建者 Fabien (@fabpot) 的说法,它实际上仅用于调试目的,因此我不会为了将来的升级而依赖它。

Sidenote

边注

Remember to avoid $request->get()anytime you can, so no $request->get('_route')as I've seen in some answers on similar questions

记得尽可能避免$request->get(),所以不要,$request->get('_route')因为我在类似问题的一些答案中看到过

If you don't need the flexibility in controllers, it is better to explicitly get request parameters from the appropriate public property instead (attributes, query, request)

如果您不需要控制器的灵活性,最好从适当的公共属性中显式获取请求参数(属性、查询、请求)

The reason being that it will look in said public properties (attributes, query & request) instead of just the one (attributes), making it much slower

原因是它将查看所​​述公共属性(属性、查询和请求),而不仅仅是一个(属性),使其速度慢得多

回答by BentCoder

Not a good thing to do directly in Twig but you can still do. The better way is to pass it as an argument from the controller.

直接在 Twig 中做不是一件好事,但您仍然可以这样做。更好的方法是将它作为来自控制器的参数传递。

Get route parameters in Twig.

在 Twig 中获取路由参数。

{{ app.request.attributes.get('_route_params') }}

AND

Gets whole bundle name in Twig.

在 Twig 中获取整个包名称。

{{ app.request.attributes.get("_controller") }}

Get route name in Twig.

在 Twig 中获取路线名称。

{{ app.request.attributes.get('_route') }}

回答by kisanme

To get the Route Name in Symfony2 enter the following code snippet

要在 Symfony2 中获取路由名称,请输入以下代码片段

$request = $this->container->get('request');
$routeName = $request->get('_route');

To get the URL in Symfony2,

要在 Symfony2 中获取 URL,

$request = $this->container->get('request');
$routeURL = $request->getRequestUri();

回答by scandel

Adding that in some cases, app.request.uriwon't return the url of the current page.

添加在某些情况下,app.request.uri不会返回当前页面的 url。

Example: in your page template, you call a controller via:

示例:在您的页面模板中,您通过以下方式调用控制器:

{{ render(controller('AppBundle:MyController:myBlock')) }}

And in myBlockAction, you render another template, say block.html.twig.

在 中myBlockAction,您渲染另一个模板,例如block.html.twig

A call to app.request.urifrom block.html.twigwill display something like:

app.request.urifrom的调用block.html.twig将显示如下内容:

http://www.example.com/app.php/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3DAppBundle%253AMyController%253AmyBlock

If you want to get the absolute url of the rendered page from inside block.html.twig, you can reassemble it from php $_SERVER variables:

如果你想从 inside 获取渲染页面的绝对 url block.html.twig,你可以从 php $_SERVER 变量重新组装它:

{{ app.request.server.get('REQUEST_SCHEME') ~ '://' ~ 
   app.request.server.get('SERVER_NAME') ~ 
   app.request.server.get('PHP_SELF') }}

You can also add QUERY_STRINGif needed.

QUERY_STRING如果需要,您也可以添加。