1
votes

I created a Laravel Api-rest, but when i'm accessing the api endpoint it's responding me 404 not found. I added the endpoint in api.php

The url that I'm accessing is localhost:8000/api/my_first_api but the browser returns 404 not found.

Route::get('my_first_api',
   'HomeController@my_first_api')>name('my_first_api');

HomeController.php :

public function my_first_api()
{
   $home_content = DB::select('SELECT * FROM content_structure WHERE content_pages = ? ',[
        'Home'
   ]);

    return response()->json(array(['data' => $home_content]));
}

RouteServiceProvider.php :

protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware('api')
         ->namespace($this->namespace)
         ->group(base_path('routes/api.php'));


}

My Route List: Router

1
If you are using laravel-5.5 then you need to use laravel resource api laravel.com/docs/5.5/eloquent-resources - Ayaz Ali Shah
@AyazShah is that recommended to use eloquent rather than query builder? - DevGe
You can use both of them but eloquent would be good approach - Ayaz Ali Shah
ok, but i try first the builder to test if it's working.. do you have idea why is not working for that? - DevGe
try php artisan route:list command and see if your route is there - Haseeb Zulfiqar

1 Answers

1
votes

Please Check Your Code

 Route::get('my_first_api',
   'HomeController@my_first_api')->name('my_first_api');

And Just add public in url before api.

localhost:8000/public/api/my_first_api