Part 5 – Setting up the Routes
Now that our models are setup, we are now ready to begin configuring the routes. This is basically the part where we tell the application what to do once it receives an HTTP request. All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the API middleware group.
The main benefit with this approach is that we get the suggestion and easy implementation of the distinction between our web routes and our API routes. For most applications, you will begin by defining routes in your routes/web.php file. But in our case, we are building a microservice that will accessed through an API so we will go ahead and modify the file routes/api.php. In order to build a route, you can use the Route:: façade and specify the HTTP action that you whish to do. The basic actions such as GET, POST, PUT and DELETE are all supported.
Typically, in a REST architecture the HTTP requests match up like so:
- GET – retrieve resource
- POST – create new resource
- PUT – update existing resource
- DELETE – delete resource
The table below from restful-api-design lists the standard methods that have a well-defined meaning for all resources and collections.
Method | Scope | Semantics |
---|---|---|
GET | collection | Retrieve all resources in a collection |
GET | resource | Retrieve a single resource |
HEAD | collection | Retrieve all resources in a collection (header only) |
HEAD | resource | Retrieve a single resource (header only) |
POST | collection | Create a new resource in a collection |
PUT | resource | Update a resource |
PATCH | resource | Update a resource |
DELETE | resource | Delete a resource |
OPTIONS | any | Return available HTTP methods and other options |
So we will go ahead and build a hamburgers route that uses a GET request. We can use a closure to define what happens for our route.
Route::get('hamburgers', function () { return App\Hamburger::all(); }
This tells the application to returns all the hamburgers in our database using a route called ‘hamburgers’ that is accessible through a ‘GET’ request. As you can tell, we are using the hamburger model along with the all() method. The all() method is a predefined eloquent query that is available to all models. It’s sort of the same as the SQL statement “SELECT * FROM ..”.
Since I am using xampp, I can view the output on my localhost at http://localhost/burgerpedia/public/api/hamburgers Going to this URL using Postman (my favorite Chrome Plugin!!!) shows the following output:
By default, Laravel turns every output into JSON. So if a collection is returned, it is always serialized into a JSON collection. So the final output is a JSON array containing the hamburgers.
Now, you may want to predefine the name of some routes that can be used anywhere else in the application. To do this, you can add an additional, optional array before the closure. Inside this array, we can use the ‘as’ namevalue to define the name like so:
Route::get('hamburgers', ['as' => 'hamburgers_api', function () { return App\Hamburger::all(); }] );
Now, if this path ever changes, nothing in the code has to be modified since all we need to do is reference the name hamburgers_api.
This is not how you should setup typical routes for an application but its a good way to understand how routing works in Laravel. Now we will talk about controllers and setup proper routing for our application.
Previous Step: Creating the Models and the Relationships
Next Step: Creating the Controllers