0

I have question regarding registering the api on the route.php I got an error accessing my api that I already created on the route.php

Class App\Http\Controllers/api/UserController does not exist

I create api folder I named it as api inside Controllers/api I create UserController.php

My Function:

public function index() {

    $table_slider = DB::select('SELECT content_id,link,file,slider_sorting,content_pages FROM content_structure as cs LEFT JOIN (SELECT cid,file FROM content_upload_assets) cua ON cs.content_id = cua.cid WHERE content_pages = ? AND cs.status = ? ORDER BY content_id DESC ',[

        'Slider',
        'Active'

    ]);
    return response()->json($table_slider);
}

On my api.php

Route::get('index','/api/UserController@index');

To solved the issue I used this route.

Route::get('index','api\UserController@index')

DevGe
  • 1,381
  • 4
  • 35
  • 66

3 Answers3

3

I think you should double check your api route. As per my understanding your route declaration should be corrected as follows.

Route::get('index','\api\UserController@index');

Here is more related topic to refer.Laravel Controller Subfolder routing

Think this will help.

1

You're using the wrong directory separator:

Route::get('index','/api/UserController@index')

Change it to

Route::get('index','\api\UserController@index')
Brian Lee
  • 17,904
  • 3
  • 41
  • 52
1

You are using wrong way to point the controller, You can change your route like this:

Route::get('index','api\UserController@index');

A much better way is using namespace. Your controller should look like,

<?php

namespace App\Http\Controllers\api;

class UserController extends Controller
{
    public function index() {

    $table_slider = DB::select('SELECT content_id,link,file,slider_sorting,content_pages FROM content_structure as cs LEFT JOIN (SELECT cid,file FROM content_upload_assets) cua ON cs.content_id = cua.cid WHERE content_pages = ? AND cs.status = ? ORDER BY content_id DESC ',[

    'Slider',
    'Active'

    ]);

    return response()->json($table_slider);
}

Now, you can specify namespace along with route as:

Route::group(['namespace' => 'api'],function(){
    Route::get('index','UserController@index');
});

If any confusion feel free to ask.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84