简体   繁体   中英

How to register api on the routes of laravel

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

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.

You're using the wrong directory separator:

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

Change it to

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM