目标类控制器不存在-Laravel 8 [英] Target class controller does not exist - Laravel 8

查看:182
本文介绍了目标类控制器不存在-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');
    }
}

如屏幕截图所示,该类存在并且位于正确的位置:

As seen in the screenshot, the class exists and is in the correct place:

我的 api.php 路由:

My api.php route:

Route::get('register', 'Api\RegisterController@register');

当我使用邮递员访问 register 路由时,它给了我以下错误:

When I hit my register route using Postman, it gave me the following error:

目标类[Api \ RegisterController]不存在.

Target class [Api\RegisterController] does not exist.


更新:

多亏了答案,我才得以解决.我决定为此路线使用完全限定的类名,但答案中还介绍了其他选项.

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', 'App\Http\Controllers\Api\RegisterController@register');

推荐答案

您正在使用Laravel8.在Laravel 8的全新安装中,没有名称空间前缀应用于您的路由加载到的路由组./p>

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 helper/ URL :: action 方法.在Laravel 8.x中,默认情况下此属性为 null .这意味着Laravel不会自动进行名称空间前缀. Laravel 8.x文档-发行说明

"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 the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel." Laravel 8.x Docs - Release Notes

在不使用名称空间前缀的情况下,在路由中引用控制器时,必须使用控制器的完全合格类名称.

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 App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
// or
Route::get('/users', 'App\Http\Controllers\UserController@index');

如果您喜欢旧方法:

App \ Providers \ RouteServiceProvider :

public function boot()
{
    ...

    Route::prefix('api')
        ->middleware('api')
        ->namespace('App\Http\Controllers') // <---------
        ->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.

此信息现在在《升级指南》中

Laravel 8.x文档-升级指南-路由

升级指南显示的重要部分是,您在路由组上定义了名称空间.单独设置 $ 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆