Specifying the HTTP method for Laravel routes

Updated: January 18, 2024 By: Guest Contributor Post a comment

Overview

Routes are a critical component of any web application, defining the pathways by which client requests are handled. Laravel, as one of the leading PHP frameworks, provides an elegant routing system that is both flexible and expressive. In this tutorial, we will delve into the specifics of routing in Laravel, with a focus on specifying the different HTTP methods.

HTTP methods or verbs such as GET, POST, PUT, DELETE, and so on, indicate the desired action to be performed on a given resource. In Laravel, routes are typically defined in the routes/web.php file for web requests, and in the routes/api.php file for API requests. By default, Laravel supports all the common HTTP methods, and specifying them in your routes is straightforward. Let’s start with the basic route definition.

Basic GET Route

Route::get('/articles', function () {
    return 'Article Index';
});

This route responds to a GET request at /articles URL with a closure, which returns a plain string ‘Article Index’. Now, let’s see how to specify other HTTP methods in Laravel routes.

POST Route

Route::post('/articles', function () {
    // Code to handle a POST request to add a new article
});

For creating resources, such as submitting a form to create a new article, a POST route is used.

PUT/PATCH Route

Route::put('/articles/{article}', function ($article) {
    // Code to update the article
});

Route::patch('/articles/{article}', function ($article) {
    // Code to partially update the article
});

These routes respond to PUT and PATCH requests and are generally used for updating resources. The difference between PUT and PATCH is subtle: PUT replaces the resource entirely, whereas PATCH applies a partial update.

DELETE Route

Route::delete('/articles/{article}', function ($article) {
    // Code to delete the article
});

When you need to remove a resource, you would specify a DELETE route.

Resource Controller

Route::resource('photos', 'PhotoController');

Beyond individual route declarations, Laravel provides the resource controller, which creates multiple routes to handle a range of actions on a resource. You can register a resourceful route to a controller, and Laravel will handle the HTTP method determination internally.

Form Method Spoofing

Note: HTML forms do not support PUT, PATCH, or DELETE actions. So, when defining these in a Laravel application, you should use a POST route in the form, and include a hidden ‘_method’ field to spoof these HTTP verbs.

<form action="/articles/1" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <!-- form fields -->
    <!-- CSRF field -->
<form>

The _method field lets Laravel know what HTTP method should be simulated.

Route Naming

To refer to your routes more comfortably within your application, you can assign names to them.

Route::get('/articles/create', 'ArticleController@create')->name('articles.create');

This allows you to refer to the path by its name rather than the URL itself through the route helper function.

See the detailed guide here: How to Implement Rate Limiting in Laravel.

Route Grouping

You can also group routes that share attributes, such as middleware or namespaces, making your code cleaner and more structured.

Route::middleware(['auth'])->group(function () {
    Route::get('/profile', function () {
        // Uses 'auth' middleware
    });

    Route::post('/profile', function () {
        // Uses 'auth' middleware
    });
});

This structure uses a closure to contain the grouped routes.

Middleware

Middleware is a mechanism for filtering HTTP requests in Laravel. You can specify middleware for your routes, thereby imposing certain conditions that the request must satisfy before being handled by the route.

Route::get('/admin', function () {
    // This route is now protected by the 'auth' middleware
})->middleware('auth');

Final Words

Understanding and specifying different HTTP methods in Laravel is a cornerstone of robust application design, allowing you to effectively handle the CRUD operations (Create, Read, Update, Delete) and establish clear patterns in your route definitions. This tutorial walked you through the practical aspects of these methods within your Laravel routes, offering you foundational knowledge that you can build upon as you architect the back-end of your web applications.