Laravel 5.4 Upgrade: Middleware Class Does Not Exist

If you’ve finally made the upgrade to Laravel 5.4, you may have been greeted with an ambiguous error message like this one:

screenshot: laravel error

Middleware nowhere to be found in Laravel 5.4

If you’ve hit this error, odds are you are registering middleware in a service provider or in your application at runtime. Perhaps something like this:

screenshot: code sample of offending error

The $router->middleware() method in action in Laravel 5.3

The underlying issue is that the Laravel router class no longer has the middleware method, instead favoring the naming aliasMiddleware. Simply change the method name and you’re good to go.

1
2
3
4
5
// In with the new
$router->aliasMiddleware('admin', 'MyPackage\Http\Middleware\Admin');
 
// Out with the old
$router->middleware('admin', 'MyPackage\Http\Middleware\Admin');
// In with the new
$router->aliasMiddleware('admin', 'MyPackage\Http\Middleware\Admin');

// Out with the old
$router->middleware('admin', 'MyPackage\Http\Middleware\Admin');
Discussion

Leave a Reply