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

chrisgrim's avatar

Laravel 11 Middleware

Im not sure what I am doing wrong. I am trying to register a custom middleware. I made the middleware RedirectIfPaid and here is my file

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class RedirectIfPaid
{
    public function handle(Request $request, Closure $next): Response
    {
        if (auth()->check() && auth()->user()->has_paid) {
            return redirect('/'); 
        }

        return $next($request);
    }
}

then I went to bootstrap/app and added it there

use App\Http\Middleware\RedirectIfPaid;

->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'redirectIfPaid' => RedirectIfPaid::class,
        ]);
    })

then I ran

php artisan config:clear
php artisan cache:clear
php artisan route:clear

and in my web.php routes file I have

Route::get('/checkout', [PurchaseController::class, 'index'])
                ->middleware('redirectIfPaid')
                ->name('purchase.index');

and I am getting the error

Illuminate
 \ 
Contracts
 \ 
Container
 \ 
BindingResolutionException
PHP 8.3.3
11.1.1
Target class [redirectIfPaid] does not exist.

What am I doing wrong?

0 likes
10 replies
LaryAI's avatar
Level 58

It seems like you are trying to register your middleware in the wrong place. In Laravel, middleware can be registered either globally, as route middleware, or as part of a middleware group. The code snippet you provided seems to be attempting to register the middleware in a non-standard way.

Here's how you can properly register your RedirectIfPaid middleware:

  1. Open the app/Http/Kernel.php file.

  2. Add your middleware to the $routeMiddleware array if you want to use it on specific routes only (which seems to be your case). This array is used to define your route middleware.

Here's an example of how to add your middleware to the $routeMiddleware array:

protected $routeMiddleware = [
    // ...
    'redirectIfPaid' => \App\Http\Middleware\RedirectIfPaid::class,
    // ...
];

After adding your middleware to the $routeMiddleware array, you should be able to use it in your routes like this:

use App\Http\Controllers\PurchaseController;

Route::get('/checkout', [PurchaseController::class, 'index'])
    ->middleware('redirectIfPaid')
    ->name('purchase.index');

Make sure you don't have any typos in your middleware class name or the alias you are using in your routes. Also, ensure that the namespace for your middleware class is correct.

After making these changes, you shouldn't need to manually clear the config, cache, or routes, but it's still a good practice to do so after changes to ensure that Laravel picks up the new configuration. You can run the following Artisan commands:

php artisan config:clear
php artisan cache:clear
php artisan route:clear

Try these steps, and your middleware should be registered correctly and the error should be resolved.

Snapey's avatar

I know its beside the point, but creating an alias is just an unnecessary complication

The alias lets you refer to your middleware as redirectIfPaid instead of RedirectIfPaid. Hardly a shorter alias!

But, the real issue is that you have not registered the middleware at all, just created an alias for it.

I can't quite get my head around the purpose of making the kernel.php disappear, but I think you just need to undo changes to the boostrap/app file and change your route file to;

use App\Middleware\RedirectIfPaid;

//

Route::get('/checkout', [PurchaseController::class, 'index'])
                ->middleware('RedirectIfPaid::class')
                ->name('purchase.index');

But TBH for a simple check like this I would just do the check in your purchase controller. Midlleware for a single endpoint is an overkill.

chrisgrim's avatar

Hi @snapey !! I guess I assumed that registering it as an Alias was registering it. I am unclear about how to register middleware in Laravel 11 now that the Kernel is gone.

chrisgrim's avatar

do you mean dont register it? Instead just load it in the route file?

sidanalavi's avatar

update

use App\Http\Middleware\RedirectIfPaid;

->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'redirectIfPaid' => RedirectIfPaid::class,
        ]);
    })

to below

use \App\Http\Middleware\RedirectIfPaid;

->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'redirectIfPaid' => RedirectIfPaid::class,
        ]);
    })

it worked in my case

Please or to participate in this conversation.