问题描述
这是我的控制器:
<?php
namespace AppHttpControllersApi;
use AppHttpControllersController;
use IlluminateHttpRequest;
class RegisterController extends Controller
{
public function register(Request $request)
{
dd('aa');
}
}
如截图所示,该类存在并且位于正确的位置:
As seen in the screenshot, the class exists and is in the correct place:
我的api.php
路由:
My api.php
route:
Route::get('register', 'ApiRegisterController@register');
当我使用 Postman 访问我的 register
路由时,它给了我以下错误:
When I hit my register
route using Postman, it gave me the following error:
目标类 [ApiRegisterController] 不存在.
更新:
多亏了答案,我才能够修复它.我决定为此路由使用完全限定的类名,但还有其他选项,如答案中所述.
Thanks to the answer, I was able to fix it. I decided to use the fully qualified class name for this route, but there are other options as described in the answer.
Route::get('register', 'AppHttpControllersApiRegisterController@register');
推荐答案
您正在使用 Laravel 8.在 Laravel 8 的全新安装中,没有将命名空间前缀应用于您的路由加载到的路由组.
You are using Laravel 8. In a fresh install of Laravel 8, there is no namespace prefix being applied to your route groups that your routes are loaded into.
"在以前的 Laravel 版本中,RouteServiceProvider
包含一个 $namespace
属性.此属性的值将自动添加到控制器路由定义和对 action
帮助程序/URL::action
方法的调用的前缀.在 Laravel 8.x 中,该属性默认为 null
.这意味着 Laravel 不会自动添加命名空间前缀."Laravel 8.x 文档 - 发行说明
如果不使用命名空间前缀,则在路由中引用控制器时,您必须使用完全限定的类名.
You would have to use the Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing.
use AppHttpControllersUserController;
Route::get('/users', [UserController::class, 'index']);
// or
Route::get('/users', 'AppHttpControllersUserController@index');
如果您更喜欢旧方式:
AppProvidersRouteServiceProvider
:
public function boot()
{
...
Route::prefix('api')
->middleware('api')
->namespace('AppHttpControllers') // <---------
->group(base_path('routes/api.php'));
...
}
对您想要为其声明命名空间的任何路由组执行此操作.
Do this for any route groups you want a declared namespace for.
$namespace
属性:
The $namespace
property:
尽管在发行说明中提到了要在 RouteServiceProvider
上设置的 $namespace
属性,并在您的 RouteServiceProvider
中对此进行了评论对您的路线没有任何影响.它目前仅用于添加命名空间前缀以生成动作的 URL.所以你可以设置这个变量,但它本身不会添加这些命名空间前缀,你仍然需要确保在将命名空间添加到路由组时使用这个变量.
Though there is a mention of a $namespace
property to be set on your RouteServiceProvider
in the Release notes and commented in your RouteServiceProvider
this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it by itself won't add these namespace prefixes, you would still have to make sure you would be using this variable when adding the namespace to the route groups.
此信息现在在升级指南中
升级指南显示的重要部分是您在路由组上定义命名空间.单独设置 $namespace
变量仅有助于生成操作的 URL.
With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace
variable by itself only helps in generating URLs to actions.
再次强调,重要 部分是为路由组设置命名空间,它们恰好通过引用成员变量 $namespace
直接在示例中.
Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace
directly in the example.
如果您从 laravel/laravel
的 8.0.2 版开始安装了 Laravel 8 的新副本,您可以取消注释 中的
回到旧的方式,因为路由组被设置为使用这个成员变量作为组的命名空间.protected $namespace
成员变量RouteServiceProvider
If you have installed a fresh copy of Laravel 8 since version 8.0.2 of laravel/laravel
you can uncomment the protected $namespace
member variable in the RouteServiceProvider
to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.
// protected $namespace = 'App\Http\Controllers';
取消注释将命名空间前缀添加到分配给路由的控制器的唯一原因是因为路由组设置为使用此变量作为命名空间:
The only reason uncommenting that would add the namespace prefix to the Controllers assigned to the routes is because the route groups are setup to use this variable as the namespace:
...
->namespace($this->namespace)
...
这篇关于目标类控制器不存在 - Laravel 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!