Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use API Routes in Laravel 5.3

In Laravel 5.3 API routes were moved into the api.php file. But how can I call a route in api.php file? I tried to create a route like this:

Route::get('/test',function(){      return "ok";  }); 

I tried the following URLs but both returned the NotFoundHttpException exception:

  • http://localhost:8080/test/public/test
  • http://localhost:8080/test/public/api/test

How can I call this API route?

like image 489
gw0 Avatar asked Sep 16 '16 21:09

gw0


People also ask

What is difference between api and web routes Laravel?

In a Laravel application, you will define your “web” routes in routes/web. php and your “API” routes in routes/api. php. Web routes are those that will be visited by your end users; API routes are those for your API, if you have one.


1 Answers

You call it by

http://localhost:8080/api/test                       ^^^ 

If you look in app/Providers/RouteServiceProvider.php you'd see that by default it sets the api prefix for API routes, which you can change of course if you want to.

protected function mapApiRoutes() {     Route::group([         'middleware' => 'api',         'namespace' => $this->namespace,         'prefix' => 'api',     ], function ($router) {         require base_path('routes/api.php');     }); } 
like image 194
peterm Avatar answered Sep 19 '22 13:09

peterm