Support the ongoing development of Laravel.io →
Laravel
<?php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */

     public function handle(Request $request, Closure $next, string ...$guards)
     {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                return redirect()->route('account.profile');
            }
        }
 
         return $next($request);  // This triggers the error
     }
}

Hey, I have this middleware that only gives me problems during my live server. I get the following error whenever this middleware is triggered:

Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Illuminate\Routing\Redirector given

The error points towards return $next($request);

Can someone help me fix this issue? THank You in advance

Last updated by @foreverlove0114 1 year ago.
0

I am also having this exact same problem! It started happening yesterday, whereas it was working perfectly fine the week before; the error is getting thrown on my custom auth middleware on the return $next($request); line. I'm going out of my mind trying to figure out what I did wrong!

Update: I kind of fixed it?? At least I'm not getting the error in my custom auth anymore... What I had to do was manipulate what was being returned by my API endpoint that I was hitting, as what it was trying to send back was the wrong datatype that my code was expecting. Check your route that you're hitting that's causing that error and try changing it to return something else. It sounds like it's coming from a redirect, so maybe the problem is in your web.php file??

Last updated by @bigdogdman 10 months ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.