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】Requestクラスのインスタンスにリクエストヘッダーを追加する

Posted at

確認したバージョン: Laravel 6.20

やりかた

$request->headersetメソッドで実現できます。

sample.php
<?php
use Illuminate\Http\Request;

// リクエスト生成
$request = new Request;
// Request Header追加(例として「Authorization: Bearer test_token」を追加します)
$request->headers->set('Authorization', 'Bearer ' . 'test_token'); 

dd($request); //出力

結果

Illuminate\Http\Request^ {#676
  #json: null
  
//省略//

  +headers: Symfony\Component\HttpFoundation\HeaderBag^ {#123
    #headers: array:1 [
      "authorization" => array:1 [
        0 => "Bearer test_token"
      ]
    ]
    #cacheControl: []
  }

//省略//

}

headersプロパティの中に新たにauthorizationが追加されました。

参考

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?