【问题标题】:ReflectionException - Middleware class does not exist Laravel 5.5ReflectionException - 中间件类不存在 Laravel 5.5
【发布时间】:2018-06-09 19:59:26
【问题描述】:

我知道我不是第一个遇到这种问题的人。我已经阅读了有关 Stackoverflow 和其他来源的所有现有信息,但它无法解决我的问题,我总是会收到 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);
    }
}

不开玩笑,这真的是我的课。我已将其重命名为 xxx 以避免拼写错误。

我所有的路由都会通过webadmin中间件:

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

这是我的/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,
    ],  
  • 文件名为xxx.php,位置为App\Http\Middleware\
  • 命名空间正确
  • composer dump-autoload 无法解决问题

我也尝试修改我的composer.json:

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

导致以下警告:

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.

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    首先,将命名空间更改为:

    namespace App\Http\Middleware;
    

    然后从composer.json 中删除"App/Http/Middleware/xxx.php" 并运行composer du

    如果您使用的是 5.2.27 及更高版本,您还需要remove web middleware from the routes file

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

    【讨论】:

      【解决方案2】:

      将您的命名空间更改为 -

      namespace App\Http\Middleware;
      

      【讨论】:

        【解决方案3】:

        您可能在bootstrap/cache 目录中缓存了文件。删除那些并再次作曲家 dumpautoload。

        如果同样失败,请删除整个供应商目录并删除 composer.lock,然后运行 ​​composer install

        【讨论】:

          猜你喜欢
          • 2020-03-17
          • 1970-01-01
          • 1970-01-01
          • 2018-04-11
          • 2015-12-05
          • 2014-01-24
          • 2022-10-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多