Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Steady-Entertainment's avatar

PHP Fatal error: in Authenticate::redirectTo

Since I upgraded my system to php8.0 and i also did a composer update and composer install inside my project I am getting the following error:


PHP Fatal error:  Declaration of App\Http\Middleware\Authenticate::redirectTo(Illuminate\Http\Request $request) must be compatible with Illuminate\Auth\Middleware\Authenticate::redirectTo($request) in /Users/reniar/code/LivewireTest/app/Http/Middleware/Authenticate.php on line 16
PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php:1038
Stack trace:
#0 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php(839): Illuminate\Container\Container->notInstantiable('Illuminate\Cont...')
#1 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php(712): Illuminate\Container\Container->build('Illuminate\Cont...')
#2 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(796): Illuminate\Container\Container->resolve('Illuminate\Cont...', Array, true)
#3 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Foundation\Application->resolve('Illuminate\Cont...', Array)
#4 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(781): Illuminate\Container\Container->make('Illuminate\Cont...', Array)
#5 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(164): Illuminate\Foundation\Application->make('Illuminate\Cont...')
#6 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(108): Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler()
#7 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(94): Illuminate\Foundation\Bootstrap\HandleExceptions->renderForConsole(Object(Illuminate\Contracts\Container\BindingResolutionException))
#8 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(130): Illuminate\Foundation\Bootstrap\HandleExceptions->handleException(Object(Illuminate\Contracts\Container\BindingResolutionException))
#9 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleShutdown()
#10 {main}
  thrown in /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 1038

Fatal error: Declaration of App\Http\Middleware\Authenticate::redirectTo(Illuminate\Http\Request $request) must be compatible with Illuminate\Auth\Middleware\Authenticate::redirectTo($request) in /Users/reniar/code/LivewireTest/app/Http/Middleware/Authenticate.php on line 16

Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php:1038
Stack trace:
#0 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php(839): Illuminate\Container\Container->notInstantiable('Illuminate\Cont...')
#1 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php(712): Illuminate\Container\Container->build('Illuminate\Cont...')
#2 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(796): Illuminate\Container\Container->resolve('Illuminate\Cont...', Array, true)
#3 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Foundation\Application->resolve('Illuminate\Cont...', Array)
#4 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(781): Illuminate\Container\Container->make('Illuminate\Cont...', Array)
#5 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(164): Illuminate\Foundation\Application->make('Illuminate\Cont...')
#6 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(108): Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler()
#7 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(94): Illuminate\Foundation\Bootstrap\HandleExceptions->renderForConsole(Object(Illuminate\Contracts\Container\BindingResolutionException))
#8 /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(130): Illuminate\Foundation\Bootstrap\HandleExceptions->handleException(Object(Illuminate\Contracts\Container\BindingResolutionException))
#9 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleShutdown()
#10 {main}
  thrown in /Users/reniar/code/LivewireTest/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 1038

Process finished with exit code 255

my Authenticate.php looks like this

<?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.
     *
     * @param Request $request
     * @return string|null
     */
    protected function redirectTo(Request $request)
    {
        if (! $request->expectsJson()) {
            return route('auth.login');
        }
    }
}

from my understanding there is a change in the switch statement in php8.0

also I am using laravel since 2 years now or so and I kind of understand what $request->expectsJson() does but I cannot really wrap my head around what it is really meant to do?!

So if there is a request coming from the user to the server and it contains a Head of Application/json that means that the request contains JSON and what is the request now expecting the JSON or delivering the JSON? Or is the server expecting JSON?

I am really confused about this still...

0 likes
3 replies
MichalOravec's avatar
Level 75

In Authenticate middleware has to be as. So remove Request from that method

/**
 * Get the path the user should be redirected to when they are not authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return string|null
 */
protected function redirectTo($request)
{
    // your code
}
1 like
Sinnbeck's avatar

Just remove the Request type hint

protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('auth.login');
        }
    }
1 like
Steady-Entertainment's avatar

Sorry Sinnbeck, Michal was faster so he gets the best answer reward :D

Still thanks for helping me and your time ...

please note I also updated my question even if it is off topic I would love to have your explanation on how you understand the whole jsonExpects etc.

Or should I make a new thread on that?

Please or to participate in this conversation.