Target Class does not exists 解决方案

laravel

问题情况

在路由里写了一个 API 路由

1
2
3
4
5
Route::prefix('v1')->namespace('Api')->name('api.v1.')->group(function () {
// 短信验证码
Route::post('verificationCodes', [VerificationCodesController::class, 'store'])
->name('verificationCodes.store');
});

控制器如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

class VerificationCodesController extends Controller
{
public function store()
{
return response()->json(['test_message' => 'store verification code']);
}
}

Postman 以 post 方式访问 http://larabbs.test/api/v1/verificationCodes,报错:

1
Target Class XXXXXXXXX does not exists

2. 解决方案

抛开先入为主的思想,我们一条一条的排查:

  1. php artisan cache:clear
  2. 检查命名空间大小写
  3. 是否有路由组,路由组是否声明了命名空间?
  4. 在同样的位置注册另一个路由地址,使用其他控制器,是否生效?
  5. 注释关于 SearchController 的所有相关代码,项目是否正常运行?
  6. 把 api.php 路由文件的内容全部删除,项目是否正常运行?