0
0

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 1 year has passed since last update.

【Laravel】現在のURI(クエリパラメータ付き)を取得する

Last updated at Posted at 2024-01-26

結論

$request->getRequestUri()
上記で取れる。

雑な使用例

こんなかんじ

/app/Http/Controllers/HogeController.php
    public function index(Request $request)
    {
        dd($request->getRequestUri());        

↓以下のように出力されるはず
"/ルーティングで設定したドメイン省いたパス?クエリパラメーター"

余談

  • Laravelの機能ではなくPHP側でいい、ということなら$_SERVER['REQUEST_URI']でよい
  • クエリパラメーターは不要なら$request->path()でよい
  • URIではなくURL(ドメインも含めて)ということならurl()->full()でよい

さらに余談

$request->getRequestUri()は以下で定義されていた

backend/vendor/symfony/http-foundation/Request.php
    public function getRequestUri(): string
    {
        return $this->requestUri ??= $this->prepareRequestUri();
    }

なので厳密にはLaravelではなくSymfonyの機能っぽい
他のパス取得は結構情報があるけどこれだけ情報があまりなかったのはそのせいかも

おわり

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?