App \ Http \ Controllers \ v1 \ Random \ UserController类不存在

时间:2019-06-08 21:21:15

标签: php laravel

我想在以下文件夹中获取控制器:app/Http/Controllers/v1/Random/UserController.php,但出现错误:Class App\Http\Controllers\v1\Random\UserController does not exist

api.php

// App v1 API
Route::group(['prefix' => 'v1'], function () {
    require base_path('routes/api/v1.php');
});

routes / api / v1.php

Route::group(['prefix' => 'random', 'namespace' => 'v1\Random'], function(){
    Route::group(['prefix' => 'users'], function (){
        Route::get('', 'UserController@index');
    });
});

folder structure

UserController.php

namespace App\Http\Controllers;

use Faker\Generator as Faker;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class UserController extends Controller{...}

我尝试将v1.php的名称空间更改为App\Http\Controllers\v1\Random,但是复制App\Http\Controllers\v1\Random时也会出现相同的错误

1 个答案:

答案 0 :(得分:2)

尝试将控制器的命名空间更改为App\Http\Controllers\v1\Random

UserController.php

namespace App\Http\Controllers\v1\Random;

use App\Http\Controllers\Controller;
// ...

class UserController extends Controller{...}

api.php

// App v1 API
Route::group(['prefix' => 'v1'], function () {
    require base_path('routes/api/v1.php');
});

routes / api / v1.php

Route::group(['prefix' => 'random', 'namespace' => 'v1\Random'], function(){
    Route::group(['prefix' => 'users'], function (){
        Route::get('', 'UserController@index');
    });
});