16
9

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 5 years have passed since last update.

Laravel5.5から5.6に更新する際にTypeErrorに遭遇した話

Last updated at Posted at 2018-02-15

やりたいこと

Laravel5.5をLaravel5.6に更新する

環境

  • macOS HighSierra 10.13.3
  • Docker version 17.12.0-ce, build c97c6d6
  • docker-compose version 1.18.0, build 8dd22a9
  • nginx version: nginx/1.13.6
  • PHP 7.2.2 (cli) (built: Feb 6 2018 23:24:31) ( NTS )
  • Laravel Framework 5.6.3

事象

composer.jsonの以下の項目を書き換えてcomposer updateを実施しました。

composer.json
        "php": ">=7.1.3",
        "fideloper/proxy": "~4.0",
        "laravel/framework": "5.6.*",

その後、アクセスすると以下のようなエラーが出ました。

laravel.log
laravel.ERROR: Type error: Argument 2 passed to Symfony\Component\HttpFoundation\Request::setTrustedProxies() must be of the type integer, array given, called in /app/vendor/fideloper/proxy/src/TrustProxies.php on line 54 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Argument 2 passed to Symfony\\Component\\HttpFoundation\\Request::setTrustedProxies() must be of the type integer, array given, called in /app/vendor/fideloper/proxy/src/TrustProxies.php on line 54 at /app/vendor/symfony/http-foundation/Request.php:564)

対応

ミドルウェアApp\Http\Middleware\TrustProxiesを書き換えてね!って書いてありました。

app/Http/Middleware/TrustProxies.php(Laravel5.5)
    protected $headers = [
        Request::HEADER_FORWARDED => 'FORWARDED',
        Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
        Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
        Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
        Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
    ];

念のためLaravel5.6のスケルトンを拾ってくると、記述の通りメンバ変数$headersの定義がUpgradeGuide同様に配列型からString型に変わっていました。
ここはカスタムしていなかったので置き換えました。

app/Http/Middleware/TrustProxies.php(Laravel5.6)
    protected $headers = Request::HEADER_X_FORWARDED_ALL;

再度確認し、無事動作することを確認しました。

補足

カスタムしていた場合、いい感じのやつをリクエストクラスから選んで当ててあげるようです。

Illuminate\Http\Request.php
    const HEADER_FORWARDED = 0b00001; // When using RFC 7239
    const HEADER_X_FORWARDED_FOR = 0b00010;
    const HEADER_X_FORWARDED_HOST = 0b00100;
    const HEADER_X_FORWARDED_PROTO = 0b01000;
    const HEADER_X_FORWARDED_PORT = 0b10000;
    const HEADER_X_FORWARDED_ALL = 0b11110; // All "X-Forwarded-*" headers
    const HEADER_X_FORWARDED_AWS_ELB = 0b11010; // AWS ELB doesn't send X-Forwarded-Host

雑感

ちゃんとUpgradeGuideを読まない私が悪い。

16
9
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
16
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?