skip to Main Content

I’m trying to create an API in Laravel, is there an api.php file in the routes folder in Laravel? or does it have to be done manually? because when I created it the file didn’t appear

I tried looking for a solution on any website but couldn’t find a way to fix it

2

Answers


  1. You need to go to routes/api.php and write the below given code in it.

    Route::get('/demo', function () {
        return response('API Demonstration Success', 200)
                      ->header('Content-Type', 'application/json');
    });
    

    You can either call the api route by using: http://localhost:8080/api/demo OR
    http://localhost/projectname/api/demo

    Login or Signup to reply.
  2. In the routes/api.php folder the api routes are created and in the Providers/RouteServiceProvider folder it finds the default api prefix where it accesses an endpoint in the following way api/endpointexample

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search