How to route GET and POST for same pattern in Laravel?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Combine GET and POST Routing for Same Pattern in Laravel
Body:
Routing is a fundamental part of any web application that enables users to interact with different parts of your app. In Laravel 4, you might face the challenge of combining both GET and POST requests for the same URL pattern. The initial code example given illustrates the use of two separate routes for the login process: one for the GET request (accessing the login page) and another for the POST request (submitting the login form).
Route::get('login', 'AuthController@getLogin');
Route::post('login', 'AuthController@postLogin');
With this setup, you'd have to write two separate routes and controllers. However, since the URL pattern is similar for both requests, you might want them to share a single URL while still differentiating between the methods of use with a route prefix. As mentioned in the initial question, CodeIgniter provides a solution using regular expressions. In Laravel, however, we can achieve this by leveraging the power of middleware and route grouping.
1. Middleware: Create a custom middleware to handle both requests and set the appropriate method for your controllers. You could name it "AuthMiddleware" and have the following code inside its class:
public function handle($request, Closure $next)
{
if ($request->isMethod('GET')) {
return redirect()->route('login.get');
} else if ($request->isMethod('POST')) {
return $next($request);
} else {
abort(405, 'Method not allowed.');
}
}
2. Route Grouping: Here's how you can create the route group using the middleware we just created and a single URL pattern for both requests:
Route::group(['middleware' => 'auth'], function() {
Route::get('login', ['as' => 'login.get', 'uses' => 'AuthController@getLogin']);
Route::post('login', ['as' => 'login.post', 'uses' => 'AuthController@postLogin']);
});
3. Conclusion: Using the middleware and route grouping, you can now combine your routes for both GET and POST requests into a single URL pattern while maintaining distinct controller methods. This approach streamlines your application by reducing unnecessary redirects and enhances the user experience. For more in-depth information on routing in Laravel 4, refer to our comprehensive blog post on Routing Essentials at https://laravelcompany.com/blog/routing-essentials-in-laravel/. As always, don't forget that documentation is your best friend when working with any framework.
Now, your original code example in Laravel 4 will look like this:
Route::group(['middleware' => 'auth'], function() {
Route::get('login', ['as' => 'login.get', 'uses' => 'AuthController@getLogin']);
Route::post('login', ['as' => 'login.post', 'uses' => 'AuthController@postLogin']);
});
This approach allows your application to remain more organized and easier to maintain, as all the routing and controller logic is now centralized within a single group. By following best practices and structuring your code appropriately, you can ensure better readability, maintenance, and flexibility for future modifications or enhancements in your Laravel 4 app.