这个问题在这里已经有了答案:
Target class controller does not exist - Laravel 8
(19 个回答)
去年关闭。
关于 Laravel 8.x 路由的问题
现在我在 web.php 文件中添加这样的行:
use App\Http\Controllers\FirstController;
use App\Http\Controllers\SecondController;
use App\Http\Controllers\ThirdController;
然后就在 FirstController::class
上工作等等。仅使用
namespace App\Http\Controllers;
是错误的吗?而不是所有 use
在开始时一个接一个地排 x 次?谢谢。
最佳答案
相反,我会简单地取消注释 app/Providers/RouteServiceProvider.php
中的这一行这将恢复到 Laravel <8 在 'routes/web.php'
中自动为路由声明添加前缀的行为'routes/api.php'
与 App\Http\Controllers
命名空间。
/**
* 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';
此注释掉的属性可能不在您的 app/Providers/RouteServiceProvider.php
中如果您在 v8 首次发布时创建了该项目(似乎它被删除然后添加回来)如果没有,只需添加它并取消通信,并确保它在 boot
中使用方法 Prop ,它会起作用。public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) // make sure this is present
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace) // make sure this is present
->group(base_path('routes/web.php'));
});
}
关于php - 为 Laravel 8 路由定义命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69026948/