获取 Twig 模板中的当前 URL?

发布于 2025-01-08 05:11:36 字数 117 浏览 3 评论 0原文

我四处寻找代码以获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

话少情深 2025-01-15 05:11:36
{{ path(app.request.attributes.get('_route'),
     app.request.attributes.get('_route_params')) }}

如果您想将其读入视图变量:

{% set currentPath = path(app.request.attributes.get('_route'),
                       app.request.attributes.get('_route_params')) %}

app 全局视图变量包含各种有用的快捷方式,例如 app.sessionapp.security。 token.user,引用您可能在控制器中使用的服务。

{{ path(app.request.attributes.get('_route'),
     app.request.attributes.get('_route_params')) }}

If you want to read it into a view variable:

{% set currentPath = path(app.request.attributes.get('_route'),
                       app.request.attributes.get('_route_params')) %}

The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.

若无相欠,怎会相见 2025-01-15 05:11:36

获取当前网址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, 5


Get path only: {{ app.request.pathinfo }} (without parameters)


Get request uri: {{ app.request.requesturi }} (with parameters)

淡淡的优雅 2025-01-15 05:11:36

在 symfony 2.1 中,您可以使用这个:

{{ path(app.request.attributes.get('_route'), 
        app.request.attributes.get('_route_params')) }}

在 symfony 2.0 中,一种解决方案是为此编写一个 twig 扩展

public function getFunctions()
{
    return array(
        'my_router_params' => new \Twig_Function_Method($this, 'routerParams'),
    );
}

/**
 * Emulating the symfony 2.1.x $request->attributes->get('_route_params') feature.
 * Code based on PagerfantaBundle's twig extension.
 */
public function routerParams()
{
    $router = $this->container->get('router');
    $request = $this->container->get('request');

    $routeName = $request->attributes->get('_route');
    $routeParams = $request->query->all();
    foreach ($router->getRouteCollection()->get($routeName)->compile()->getVariables() as $variable) {
        $routeParams[$variable] = $request->attributes->get($variable);
    }

    return $routeParams;
}

并像这样使用

{{ path(app.request.attributes.get('_route'), my_router_params()|merge({'additional': 'value'}) }}

您不需要所有这些,除非您想向链接添加其他参数,例如在寻呼机中,或者您想要更改其中一个参数。

In symfony 2.1 you can use this:

{{ path(app.request.attributes.get('_route'), 
        app.request.attributes.get('_route_params')) }}

In symfony 2.0, one solution is to write a twig extension for this

public function getFunctions()
{
    return array(
        'my_router_params' => new \Twig_Function_Method($this, 'routerParams'),
    );
}

/**
 * Emulating the symfony 2.1.x $request->attributes->get('_route_params') feature.
 * Code based on PagerfantaBundle's twig extension.
 */
public function routerParams()
{
    $router = $this->container->get('router');
    $request = $this->container->get('request');

    $routeName = $request->attributes->get('_route');
    $routeParams = $request->query->all();
    foreach ($router->getRouteCollection()->get($routeName)->compile()->getVariables() as $variable) {
        $routeParams[$variable] = $request->attributes->get($variable);
    }

    return $routeParams;
}

And use like this

{{ path(app.request.attributes.get('_route'), my_router_params()|merge({'additional': 'value'}) }}

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.

薆情海 2025-01-15 05:11:36

您可以像这样获取 Twig 中的当前 URL:

{{ app.request.schemeAndHttpHost ~ app.request.requestUri }}

You can get the current URL in Twig like this:

{{ app.request.schemeAndHttpHost ~ app.request.requestUri }}
乄_柒ぐ汐 2025-01-15 05:11:36

应该注意的是,如果您的 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 the app.request.attributes.get('_route_params'). The path() 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.

妥活 2025-01-15 05:11:36

如果您使用 Silex 2,您将无法再访问 Request 对象。

您可以通过这种方式访问​​当前请求属性。

app.request_stack.currentrequest.attributes.get('_route')

并生成完整的当前 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.

app.request_stack.currentrequest.attributes.get('_route')

And to generate the full current URL :
path(app.request_stack.currentrequest.attributes.get('_route'), app.request_stack.currentrequest.attributes.get('_route_params'))

涫野音 2025-01-15 05:11:36

在 Shopware 6 Twig 文件中,您可以使用 Framework/Routing/RequestTransformer.php 中定义的 sw-* 属性之一:

{% set currentUrl = app.request.attributes.get('sw-original-request-uri') %}

/some/path?param=1

In a Shopware 6 Twig file, you may use one of the sw-* attributes defined in Framework/Routing/RequestTransformer.php:

{% set currentUrl = app.request.attributes.get('sw-original-request-uri') %}

/some/path?param=1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文