简体   繁体   English

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

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

My route is我的路线是

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

How I can get route name in PHP template ?如何在 PHP 模板中获取路由名称?

To get the current URL获取当前 URL

$request->getRequestUri(); or app.request.uriapp.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 forwards as you are forwarding to a controller, not to a path.您可以使用$request->attributes->get('_route')app.request.attributes.get('_route')但它不是那么可靠,例如它不会像您转发给转发一样使用控制器,而不是路径。 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. 根据创建者 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原因是它将查看所​​述公共属性(属性、查询和请求),而不仅仅是一个(属性),使其速度慢得多

Not a good thing to do directly in Twig but you can still do.直接在 Twig 中做不是一件好事,但您仍然可以这样做。 The better way is to pass it as an argument from the controller.更好的方法是将它作为来自控制器的参数传递。

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') }}

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();

Adding that in some cases, app.request.uri won't return the url of the current page. app.request.uri一点,在某些情况下, 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.uri from block.html.twig will display something like:要在通话app.request.uriblock.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:如果你想从block.html.twig内部block.html.twig渲染页面的绝对 url,你可以从 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_STRING if needed.如果需要,您还可以添加QUERY_STRING

On symfony5 you can do this.在 symfony5 上你可以这样做。

Calling a controller block and passing current url:调用控制器块并传递当前 url:

{{ render_esi(controller('App\\Controller\\Frontend\\BlockController::social',{'pageUri': app.request.uri })) }}

and

<?php

namespace App\Controller\Frontend;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class BlockController extends AbstractController {
    public function social($pageUri) {
        return $this->render('block/_social.html.twig', ['pageUri' => $pageUri]);
    }

}

Output twig: 'block/_social.html.twig'输出树枝: 'block/_social.html.twig'

<small>Current Url : {{ pageUri  }}</small>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM