6

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.
titonior
  • 147
  • 4
  • 13

3 Answers3

5

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() { 
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
3

You may have cached files in bootstrap/cache directory. Delete those and composer dumpautoload again.

If that too fails, delete your entire vendor directory and delete composer.lock and then run composer install.

2

Change your namespace to -

namespace App\Http\Middleware;
Sohel0415
  • 9,523
  • 21
  • 30