How to use API Routes in Laravel 5.3
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Laravel is a widely used PHP framework that offers various features such as routing. One of its unique aspects is the division of routes based on their purpose – web and API routes. Laravel 5.3 provides dedicated files for these route types in order to facilitate better organization and management of your application's endpoints.
Understanding Web Routes
In general, you use the 'routes/web.php' file for defining web routes. These are standard HTTP requests handled through controllers and views. This file is where you usually put all your primary application routes. Here's an example of a basic route definition:Route::get('/', function() {
return "Welcome to my website!";
});
The provided code snippet defines a 'GET' request on the root ('/') path, returning a simple welcome message. Note that this route doesn't deal with any specific API requests and is considered a web route. Accessing the URL 'http://localhost:8080/' will display this message in your browser.
Diving into API Routes
As mentioned at the start, Laravel 5.3 moved its API routes to a separate file named 'api.php.' This file is intended for handling all of your application's RESTful APIs and JSON responses. This type of route is meant to be accessed by applications instead of being displayed in a browser like web routes. To illustrate how this works, check out the following example:Route::get('/test', function() {
return "ok";
});
In this code snippet, we define an API route that listens to a 'GET' request on the '/test' path. The controller function returned by this route simply echoes out the text "ok." To access this endpoint, you would use a URL such as:
http://localhost:8080/api/test
However, accessing the above URL through your browser (instead of an API client) will result in the error 'NotFoundHttpException' since web browsers don't understand JSON responses. Your requests should be made with a proper HTTP client like Postman or Curl to work correctly. This emphasizes that API routes are meant for APIs and not standard website traffic.