0

I have setup Laravel 6 project but for some reason when php artisan route:list returns “Target class [App\Sys\Http\Controllers\Api\LocationController] does not exist." I'm new in Laravel and I can't understand why the controller doesn't work. Can anyone please help me?

Here are my code:

LocationController.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);
    }
}

My route file:

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');
});
4
  • have you created the Sys folder Commented Apr 13, 2020 at 14:47
  • App\Sys\Http\Controllers (route) is not the same as App\Http\Controllers (namespace) Commented Apr 13, 2020 at 14:48
  • you have to change the namespace from namespace App\Http\Controllers\Api; to [App\Sys\Http\Controllers\Api\; Commented Apr 13, 2020 at 14:49
  • @kunalrajput Thanks.....It is working now I have missed Sys folder. Commented Apr 13, 2020 at 15:22

2 Answers 2

2

You controller is in the App\Http\Controllers\Api, not in the App\Sys\Http\Controllers namespace. Remove the locations resource route in the namespace App\Sys\Http\Controllers group and create a new one.

Do this

...

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');
});

...
Sign up to request clarification or add additional context in comments.

Comments

1

Your controller is in App\Http\Controllers\Api and your route is pointing to App\Sys\Http\Controllers\Api.

You must change:

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

To:

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

4 Comments

Let's hope MenuController still works after these changes
but while creating route group ` Route::namespace('App\Http\Controllers') ` this is already provided by laravel so is there any meaning of adding this or creating a route group
@kunalrajput, I guess there is no point of adding this then, at least the OP's question is resolved.
I'm not sure it will working because it''' search these folders inside controller you'd have to change the property of $namespace from routeserviceprovider.php if you are trying to make it custom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.