1

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

18
  • If you are using laravel-5.5 then you need to use laravel resource api laravel.com/docs/5.5/eloquent-resources Commented Dec 12, 2018 at 5:36
  • @AyazShah is that recommended to use eloquent rather than query builder? Commented Dec 12, 2018 at 5:37
  • 1
    You can use both of them but eloquent would be good approach Commented Dec 12, 2018 at 5:39
  • ok, but i try first the builder to test if it's working.. do you have idea why is not working for that? Commented Dec 12, 2018 at 5:41
  • try php artisan route:list command and see if your route is there Commented Dec 12, 2018 at 5:44

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.