错误“ target类控制器不存在”使用Laravel 8时
这是我的控制器:,
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
public function register(Request $request)
{
dd('aa');
}
}
如屏幕截图所示,类存在并且处于正确的位置:
my
api.php
路由:
Route::get('register', 'Api\RegisterController@register');
当我点击我的寄存器
使用 Postman 错误:
目标类[API \ register Controller]不存在。
我该如何修复?
多亏了答案,我才能够修复它。我决定为此路线使用完全合格的班级名称,但是答案中还有其他选项。
Route::get('register', 'App\Http\Controllers\Api\RegisterController@register');
Here is my controller:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
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:
My api.php
route:
Route::get('register', 'Api\RegisterController@register');
When I hit my register
route using Postman, it gave me the following error:
Target class [Api\RegisterController] does not exist.
How can I fix it?
Thanks to the answers, 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 answers.
Route::get('register', 'App\Http\Controllers\Api\RegisterController@register');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
运行
PHP工匠路线时,我遇到了相同的错误:List
。就我而言,我已删除了追索权控制器,但该路线仍被定义。我必须确保在我的路线/web.php中评论了有关的课程。I faced the same error when running
php artisan route:list
. In my case I had deleted the recourse controller yet the route was still defined. I had to make sure the class in question was commented off in my routes/web.php.请添加行
在Web.php文件中。
Please add the line
in the Web.php file.
目标类[类名称]不存在。
在
/routes/web.php
中,您将缺少的控制器与
使用app \ http \ controllers \ [class name];
>Target class [ class name ] does not exist.
in
/routes/web.php
you add that missing controller with
use App\Http\Controllers\ [ class name ] ;
在Laravel 8中,指定路线已更改的方式:
In Laravel 8 the way routes are specified has changed:
我有一个错误:
解决方案:
只需检查您的类名称。它应该与您的文件名完全相同。
I had this error:
Solution:
Just check your class name. It should be the exact same of your file name.
您正在使用Laravel 8。在Laravel 8的新安装中,没有将路由装入路由的路线组上的名称空间前缀。
您必须充分利用当不使用名称空间前缀时,当您的控制器在路由中引用其控制器的合格类名称。
如果您喜欢旧的方式:
app \ provider \ routeserviceProvider
:对您想要的任何路由组来执行此操作。
$ namesspace
属性:,尽管提到
$ namespace
要在您的RouteServiceProvider
中设置发行说明并在您的RouteserviceProvider
中评论,这对您的路线没有任何影响。目前,它仅用于添加一个名称空间前缀,以将URL生成操作。因此,您可以设置此变量,但是它本身不会添加这些名称空间前缀,您仍然必须确保在将命名空间添加到路由组时会使用此变量。此信息现在在升级指南中
laravel 8.x文档 - 升级指南- 路由
使用升级指南显示重要的部分是您在路由组上定义名称空间。设置
$ namespace
自身唯一的变量有助于为操作生成URL。同样,我不能足够强调这一点,重要部分是为路由组设置名称空间,它们恰好是通过引用成员变量
$ namesspace 直接在示例中。
更新:
如果您已经安装了Laravel 8的新副本,则自
Laravel/laravel/laravel
的8.0.2版本以来,您可以取消注册保护$ namespace $ namesspace
成员routeserviceProvider
中的变量要返回旧方式,因为“路由组”设置为将此成员变量用于组的命名空间。唯一的原因是将命名空间前缀添加到分配给路由的控制器的原因是因为设置了路由组以将此变量用作命名空间:
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.
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.
If you prefer the old way:
App\Providers\RouteServiceProvider
:Do this for any route groups you want a declared namespace for.
The
$namespace
property:Though there is a mention of a
$namespace
property to be set on yourRouteServiceProvider
in the Release notes and commented in yourRouteServiceProvider
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.This information is now in the Upgrade Guide
Laravel 8.x Docs - Upgrade Guide - Routing
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.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.Update:
If you have installed a fresh copy of Laravel 8 since version 8.0.2 of
laravel/laravel
you can uncomment theprotected $namespace
member variable in theRouteServiceProvider
to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.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:
案例1
我们可以更改
api.php
和< strong>web.php
文件如下。当前我们编写
语法
的方式是,应该更改为:
案例2
首先转到文件:
app&gt;提供者&gt; RouteserviceProvider.php
替换行
受保护的$ nesspace = null;
带有保护$ namespace ='app \ http \ controllers';
然后添加行
- &gt; namespace($ this-&gt; namespace)
,如图所示图像...Case 1
We can change in
api.php
and inweb.php
files like below.The current way we write
syntax
isThat should be changed to:
Case 2
First go to the file:
app > Providers > RouteServiceProvider.php
In that file replace the line
protected $namespace = null;
withprotected $namespace = 'App\Http\Controllers';
Then add line
->namespace($this->namespace)
as shown in image...在Laravel 8中定义路线的方法是
或
资源路线变成
这意味着在Laravel 8中,默认情况下没有任何自动控制器声明。
如果要坚持旧的方式,则需要在
App \ Providers \ RouteserviceProvider.php
并在路由方法中激活。The way to define your routes in Laravel 8 is either
Or
A resource route becomes
This means that in Laravel 8, there isn't any automatic controller declaration prefixing by default.
If you want to stick to the old way, then you need to add a namespace property in the
app\Providers\RouteServiceProvider.php
and activate in the routes method.在Laravel 8中,您只需在路由\ web.php 中添加控制器名称空间
,或者转到:
app \ providers \ routeserviceProvider.php
路径并删除注释:In Laravel 8 you just add your controller namespace in routes\web.php
Or go to:
app\Providers\RouteServiceProvider.php
path and remove the comment:在Laravel 8中,默认值是删除命名空间前缀,因此您可以在Laravel 7中设置旧方法,例如:
in
routeServiceProvider.php
,添加此变量:并更新
boot
方法:In Laravel 8 the default is to remove the namespace prefix, so you can set the old way in Laravel 7 like:
In
RouteServiceProvider.php
, add this variable:And update the
boot
method:安装Laravel版本8.27.0时,我遇到了相同的错误:
错误如下:
但是,当我看到
app/providers/utareserviceProvider.php
文件时,我的启动方法中有名称空间。然后我只是对此=&gt;保护$ namespace ='app \\ http \\控制器';
。现在我的项目正在工作。
I got the same error when I installed Laravel version 8.27.0:
The error is as follows:
But when I saw my
app/Providers/RouteServiceProvider.php
file, I had namespaces inside my boot method. Then I just uncommented this =>protected $namespace = 'App\\Http\\Controllers';
.Now my project is working.
Laravel 8更新了routeserviceProvider,并用字符串语法影响路由。您可以像以前的答案一样更改它,但是建议的方法是使用动作语法,而不是使用字符串语法使用路由:
应该更改为:
Laravel 8 updated RouteServiceProvider and it affects routes with the string syntax. You can change it like in previous answers, but the recommended way is using action syntax, not using route with string syntax:
It should be changed to:
在路由文件夹中的Web.php(或api.php)中,使用/添加该缺少的控制器类。
In the web.php (or api.php) in the routes folder, use/add that missing Controller class.
如果您使用的是Laravel 8,只需复制并粘贴我的代码:
If you are using Laravel 8, just copy and paste my code:
laravel 8文档实际上比任何人都更为明确,更清楚地回答了这个问题在这里的答案中:
路由命名空间更新
在Laravel的先前发行版中,
routeserviceProvider
包含$ namespace
属性。此属性的值将自动将其前缀放在控制器路由定义上,并调用action
helper/url :: Action
方法。在Laravel 8.X中,此属性默认情况下是null
。这意味着Laravel不会完成任何自动名称空间。因此,在新的laravel 8.x应用程序中,应使用标准PHP可呼叫语法定义控制器路由定义:呼叫
action
相关方法应使用相同的可呼叫语法:如果您喜欢Laravel 7.X样式控制器路由前缀,您可以简单地将
$ namespace
属性添加到应用程序的ruteserviceProvider
。The Laravel 8 documentation actually answers this issue more succinctly and clearly than any of the answers here:
Routing Namespace Updates
In previous releases of Laravel, the
RouteServiceProvider
contained a$namespace
property. This property's value would automatically be prefixed onto controller route definitions and calls to theaction
helper /URL::action
method. In Laravel 8.x, this property isnull
by default. This means that no automatic namespace prefixing will be done by Laravel. Therefore, in new Laravel 8.x applications, controller route definitions should be defined using standard PHP callable syntax:Calls to the
action
related methods should use the same callable syntax:If you prefer Laravel 7.x style controller route prefixing, you may simply add the
$namespace
property into your application'sRouteServiceProvider
.对于解决方案,只需输入第29行:
在
App \ Providers \ RouteserviceProvider.php
文件中。态
For the solution, just uncomment line 29:
in the
app\Providers\RouteServiceProvider.php
file.Just uncomment line 29
还要检查您的路由 web.php 文件,如果您的寄存器controller正确到位。
Also check your route web.php file if your RegisterController is properly in place..
一件重要的事情要确保您在路线上的每次更改后都清除了缓存(使用Laravel 9):
One important thing to make sure you do after each change on the routes is clearing the cache (using Laravel 9):
如果您想继续使用原始的自动改正控制器路由,则可以简单地设置RouteServiceProvider中$ namespace属性的值,然后更新启动方法中的路由注册以使用$ namespace属性:
If you would like to continue using the original auto-prefixed controller routing, you can simply set the value of the $namespace property within your RouteServiceProvider and update the route registrations within the boot method to use the $namespace property:
在Laravel 8中,您可以这样使用:
In Laravel 8 you can use it like this:
只需从
utareserviceProvider
删除以下行(如果不存在),请添加它):Just uncomment the below line from
RouteServiceProvider
(if does not exists then add it):我尝试了一切,直到我尝试了第二次
重新启动服务器
I tried everything, didn't work, until I tried this 2nd time
restart server
对我来说,这是因为团队中的另一个开发人员将中间件方法添加到路线组,而忘了写名称
for me it was because another developer in the team added the middleware method to the routes group and forgot to write its name
对我来说,我不得不将路线命名更改为最近支持的格式,但正如其他人所提到的那样:
但是,它继续给我带来相同的错误,直到我运行此终端命令:
For me, I had to change my route naming to the more recently supported format, as others have mentioned:
However, it continued to give me the same errors, until I ran this terminal command:
在新鲜安装的Laravel 8上,在 app/provers/utausterservices.php 文件中:
不需要的线路
,该行应帮助您以老式的方式运行Laravel。
如果您正在将Laravel的较低版本升级到8,则可能必须
在 routeservices.php 文件中隐式添加行才能以旧的方式运行。
On a freshly installed Laravel 8, in the App/Providers/RouteServices.php file:
Uncomment line
That should help you run Laravel the old-fashioned way.
In case you are upgrading from lower versions of Laravel to 8 then you might have to implicitly add line
in the RouteServices.php file for it to function the old way.
就我而言,我遇到了相同的错误,因为我忘了在路径中大写
控制器的第一个字母。
因此,我更改了
使用app \ http \ controllers \ homecontroller;
to:
使用app \ http \ controllers \ homecontroller;
>In my case, I had the same error, because I forgot to capitalize the first letter of
controllers
in the path.So I changed
use App\Http\controllers\HomeController;
to this:
use App\Http\Controllers\HomeController;
没有人说:
作曲家dump-autoload
,然后:
php。\ Artisan Optimize:clear
No one said:
composer dump-autoload
And then:
php .\artisan optimize:clear
如果您喜欢这些路线的分组,则可以这样做:
In case if you prefer grouping of these routes, you can do it as:
确保您在路线中使用文件的正确名称。
例如:
如果您的控制器文件被命名为 user.php ,请使用用户而不是 usercontroller 来引用它。
Ensure you're using the correct name of the file in your route.
For example:
If your controller file was named User.php, make that you're referencing it with User and not UserController.
在Laravel 9中,无需在RouteserviceProvider中添加名称空间。
而不是
使用
In Laravel 9, there isn't any need to add a namespace in RouteServiceProvider.
Instead of
use