类不存在- Laravel


Class does not exist - Laravel

我遵循这个教程。我目前使用的是laravel 5.3,所以它有点过时了。我已经按照教程所说的一步一步地做了,然而,我得到

   ReflectionException in Container.php line 749:
   Class First does not exist
in Container.php line 749
at ReflectionClass->__construct('First') in Container.php line 749
at Container->build('First', array()) in Container.php line 644
at Container->make('First', array()) in Application.php line 709
at Application->make('First') in Kernel.php line 173
at Kernel->terminate(object(Request), object(Response)) in index.php line 58
at require_once('C:'xampp5'htdocs'laravel'laravel'public'index.php') in server.php line 21

一切都像教程一样。我不知道问题出在哪里

问题是您创建了FirstMiddleware,但您在这里仅将其称为First:

<?php
Route::get('/usercontroller/path',[
   'middleware' => 'First',
   'uses' => 'UserController@showPath'
]);

如官方文档所述,

如果你想给中间件分配特定的路由,你应该这样做首先在你的app/Http/Kernel.php

中给中间件分配一个密钥

因此,将此添加到您的app/Http/Kernel.php文件:

protected $routeMiddleware = [
    // the other route middlewares are defined here
    'First' => 'App'Http'Middleware'FirstMiddleware::class, // add this line
]

我认为这应该足够了。