Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReflectionException - Middleware class does not exist Laravel 5.5

I know that I am not the first one with such a problem. I have read all the existing information on Stackoverflow and other sources but it could not solve my problem that I always will get a ReflectionException Class App\Http\Middleware\xxx does not exist.

<?php

namespace App\Http\Middleware\xxx;

use Closure;

class xxx
{

    public function handle($request, Closure $next)
    {

        return $next($request);
    }
}

No joke, this is really my class. I have renamed it to xxx to avoid typo.

All my routes will pass the web and admin Middleware:

Route::middleware(['web', 'admin'])->group(function() {  

And this is my /app/Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\xxx::class,
    ],  
  • File is named xxx.php and it location is App\Http\Middleware\.
  • Namespace is correct
  • composer dump-autoload will not solve the problem

I also tried to modify my composer.json:

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories",
        "App/Http/Middleware/xxx.php"
    ],

which caused following warning:

Warning: Ambiguous class resolution, "App\Http\Middleware\xxx\xxx" was found in both "$baseDir . '/app/Http/Middleware/xxx.php" and "/code/App/Http/Middleware/xxx.php", the first will be used.
like image 946
titonior Avatar asked Feb 05 '18 08:02

titonior


2 Answers

First, change namespace to:

namespace App\Http\Middleware;

Then remove "App/Http/Middleware/xxx.php" from composer.json and run composer du

You also, need to remove web middleware from the routes file if you're using 5.2.27 and higher:

Route::middleware(['admin'])->group(function() { 
like image 187
Alexey Mezenin Avatar answered Nov 03 '22 19:11

Alexey Mezenin


Change your namespace to -

namespace App\Http\Middleware;
like image 43
Sohel0415 Avatar answered Nov 03 '22 18:11

Sohel0415