laravle7 路由报错说类不存在。Target class [xxx] does not exist.

场景:

今天新下载了一个laravel7的框架,定义了一个简单的路由login来进行访问。
`Route::get('/login', 'Login@index');`

问题:

image.png

在我不懈搜索下找到了答案。

解决方法

路由命名空间更新。在之前的版本,RouteServiceProvider 具有 $namespace 属性。 该属性的值将自动添加到控制器路由定义的前缀。在laravel8.0出来之后$namespace被默认注释了。

方案一

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);

方案二

直接到RouteServiceProvider类文件中找$namespace属性。没有就添加,有就把注释打开。
/**
 * The controller namespace for the application. * * When present, controller route declarations will automatically be prefixed with this namespace. * * @var string|null
 */ protected $namespace = 'App\\Http\\Controllers';

官网地址:https://learnku.com/docs/laravel/8.x/releases/9351#d99737

评论

暂无评论