【问题标题】:Laravel App\Http\Middleware\Class not foundLaravel App\Http\Middleware\Class 未找到
【发布时间】:2019-06-27 12:38:28
【问题描述】:

刚刚将我的 Laravel 应用程序从本地环境迁移到远程服务器上的在线开发环境。在此迁移之后,我收到一个错误:

ReflectionException thrown with message "Class App\Http\MiddleWare\NotUser does not exist"

我已删除 vendor 文件夹以及 composer.lock 并运行 composer update。还清除了bootstrap/cache 并尝试运行php artisan config:clear

从存储中清除所有 cache/* 文件。每当我尝试登录仪表板时,都会收到中间件不存在的错误。

app/Http/Middleware/NotUser.php

<?php

namespace App\Http\Middleware;

use Closure;

class NotUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        # This middleware prevents anyone who is not an admin from accessing given route or controller
        # apply by using it in the constructor, $this->middleware('is.admin');
        if ($request->user()->role->name === 'user') {
            return back()->with('error', __('Access denied'));
        }

        return $next($request);
    }
}

app/Http/Kernel.php

protected $routeMiddleware = [
...
'not.user' => \App\Http\MiddleWare\NotUser::class
];

routes/web.php

Route::group(['prefix' => 'admin', 'middleware' => ['not.user', 'auth']], function () { ... }

这在本地托管环境中运行良好。我没有问题。切换到开发环境后,我开始收到此错误,但我不知道是什么原因造成的。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    我相信命名空间是区分大小写的,所以改变这个:

    protected $routeMiddleware = [
    ...
    'not.user' => \App\Http\MiddleWare\NotUser::class
    ];
    

    到这里:

    protected $routeMiddleware = [
    ...
    'not.user' => \App\Http\Middleware\NotUser::class
    ];
    

    注意Middleware 中的大写W

    【讨论】:

    • 该死的,好球!看来我的开发服务器在这方面比我的本地服务器严格得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 2015-06-24
    相关资源
    最近更新 更多