当前位置: 首页 > 知识库问答 >
问题:

目标类[App\Sys\Http\Controllers\Api\LocationController]不存在

宦翔飞
2023-03-14

我已经设置Laravel 6项目,但由于某种原因,当php工匠路由:列表返回目标类[App\Sys\Http\Controller\Api\LocationController]不存在。我是新来的Laravel,我不明白为什么控制器不工作。有人能帮帮我吗?

这是我的代码:

位置控制器。php

<?php
namespace App\Http\Controllers\Api;

//use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Location;

class LocationController extends Controller
{

    public function index(Request $request)
    {  
        $per_page = $request->per_page ? $request->per_page : 5;
        $sort_by = $request->sort_by;
        $order_by = $request->order_by;
        return response()->json(['locations' => Location::orderBy($sort_by, $order_by)->paginate($per_page)],200);
    }

    public function store(Request $request)
    {
        $location= Location::create([
            'code' =>$request->code,
            'name' =>$request->name,
            'description' =>$request->description
        ]);
        return response()->json(['location'=>$location],200);
    }


    public function show($id)                                                                                                                                                           
    {
        $locations = Location::where('code','LIKE', "%$id%")->orWhere('name','LIKE', "%$id%")->orWhere('description', 'LIKE', "%$id%")->paginate();
        return response()->json(['locations' => $locations],200);
    }



    public function update(Request $request, $id)
    {
       $location = Location::find($id);
       $location->code  = $request->code;
       $location->name  = $request->name;
       $location->description  = $request->description;
       $location->save(); 
       return response()->json(['location'=>$location], 200);
    }

    public function destroy($id)
    {
        $location = Location::where('id', $id)->delete();
        return  response()->json(['location'=>$location],200);
    }

    public function deleteAll(Request $request){
        Location::whereIn('id', $request->locations)->delete();
        return response()->json(['message', 'Records Deleted Successfully'], 200);
    }
}

我的路由文件:

api.php

<?php

use Illuminate\Http\Request;



Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::namespace('App\Sys\Http\Controllers')->group(function () {
    Route::get('/menuslevel0',['uses' => 'MenuController@menus_level_0']);

    Route::resource('locations','Api\LocationController');
});

共有2个答案

杨景山
2023-03-14

您的控制器位于App\Http\Controllers\Api中,您的路由指向App\Sys\Http\Controllers\Api

您必须更改:

Route::namespace('App\Sys\Http\Controllers')->group(function () {
    // Your routes
});

致:

Route::namespace('App\Http\Controllers')->group(function () {
    // Your routes
});
滑乐逸
2023-03-14

你的控制器在App\Http\Controller\Api中,而不是在App\Sys\Http\Controller命名空间中。删除命名空间App\Sys\Http\Controller组中的位置资源路由,并创建一个新的。

这样做

...

Route::namespace('App\Sys\Http\Controllers')->group(function () {
    Route::get('/menuslevel0',['uses' => 'MenuController@menus_level_0']);
});

Route::namespace('App\Http\Controllers')->group(function () {
    Route::resource('locations','Api\LocationController');
});

...
 类似资料: