laravel 类不存在Laravel 8 was released yesterday with a ton of new features and changes. One of those changes was the removal of the default route namespacing.Laravel 8昨天发布，具有大量新功能和更改。...
Laravel 8 was released yesterday with a ton of new features and changes. One of those changes was the removal of the default route namespacing.
Laravel 8昨天发布，具有大量新功能和更改。 这些更改之一是删除了默认路由的命名空间。
Although this change is backward-compatible, meaning that older projects that used Laravel 7.x can easily migrate to Laravel 8.x without having to change anything, new projects created in Laravel 8 (starting 8/Sep) have to take this into account.
Up until Laravel 7, the RouteServiceProvider.php file had the following code:
What this does is tell Laravel to load the routes in routes/web.php, using the web middleware and the App\Http\Controllers namespace. This, in turn, means that whenever you declared a route using the string-syntax, Laravel would look for that controller in the App\Http\Controllers folder:
If you try to run your app again, everything should be working.
使用完整的名称空间 (Using the full namespace)
This one involves changing all your route declarations, but the idea is simple: prepend your controller names with their namespace. See the following example for our PostsController inside the app/Http/Controllers folder.
If you try again, everything should be running smoothly.
使用动作语法 (Using the Action Syntax)
This is the alternative I personally recommend as I find it more typo-proof and in my experience provides better IDE support as we are explicitly telling the code which class to use. Instead of using our usual string syntax, we can use the action syntax where we specify the class and method to use in an array:
Notice here we are not passing the PostsController within quotes but rather PostsController::class, which internally will return ‘App\Http\Controllers\PostsController’. The second value in the array is the method to call within that controller, meaning: “In PostsController.php call the ‘all’ method.
Whether you added the namespace manually, specified the full namespace in your routes, or went with the action syntax, what you just did is tell Laravel in which namespace your controllers actually are, so now it actually knows where to look.