見出し画像

【Laravel10】Header may not contain more than a single header, new line detectedでハマった話

上記エラーが出て解決方法がわからず、時間がかかっていたので備忘録で。

app/Http/Middleware/Authenticate.phpをredirectToの書き方が間違っていました。

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     */
    protected function redirectTo(Request $request): ?string
    {
        return redirect('/login'); // ここが原因
    }
}

よくよく見るとresponseがstringって指定されているのがわかるかと思います。
なので、routeで文字列を返してあげればokです。

    protected function redirectTo(Request $request): ?string
    {
        return route('login');
    }

わかってみれば単純なことですが、だいぶ時間がかかってしまいました。
参考になれば幸いです。

なぜこんな風に書いてしもうたのか・・・

この記事が気に入ったらサポートをしてみませんか?