1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

symfony の twig で現在のURLを取得する方法

Posted at

はじめに

現在、表示するページのURLがほしい時が時たまある。
簡単そうなのを見つけたのでメモ

TL;DR

{{ app.request.getUri }}

これで現在のURLが取得できる。

説明

SymfonyのTwigでは、グローバル変数が定義されている。

ここにapp.requestというのがある。

いろいろなリクエストデータが取得できるようになっています。
https://symfony.com/doc/current/components/http_foundation.html#accessing-request-data

{{ dump(app.request) }}

出力結果は、下記。もう、大体のものが読めます。

2021-03-11_00h17_55.png

これでrequestUriで、絶対パスが取得できますけど、ホスト名とかHTTPとかついていて欲しいわけです。

メソッドにあるgetUriを読み出すと、いい感じでフルURLが取得できます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?