Check if request is GET or POST

Check if request is GET or POST

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Inspecting HTTP Methods in Laravel: Beyond $_POST

The snippet you provided relies on a classic pattern found in early PHP development and even some older Laravel tutorials: checking if(!empty($_POST)). While this works, it is not the idiomatic way to handle request inspection in modern Laravel. The global superglobal array $_POST exposes raw data from the server side, bypassing the framework's request lifecycle entirely. This approach is fragile because it doesn't validate the HTTP method, nor does it handle form submissions that might have empty arrays or use different content types.

Laravel provides a dedicated interface for this exact scenario through the $request object (or Request facade). The most direct way to check if a request is a POST is by examining the method() property or using the isMethod() helper.

use Illuminate\Http\Request;

public function store(Request $request)
{
    if ($request->isMethod('POST')) {
        // Handle form submission logic here
        if (Auth::attempt(['data' => $request->get('data')])) {
            return redirect()->intended();
        }
        
        Session::flash('error_message', 'Invalid credentials.');
    }
}

Using Request as a constructor argument is the preferred approach in Laravel. It gives you access to the validated input, headers, and cookies in a single place. The isMethod('POST') call explicitly checks the HTTP verb sent by the client. This is distinct from checking $_POST, which only tells you if data was submitted via a form, but not necessarily that the request intended to be a POST operation (though they usually correlate).

There is a significant nuance to consider regarding how Laravel handles GET requests with query parameters. If a user navigates to ?data=example in a browser, $_POST will be empty, and isMethod('GET') will return true. However, if you submit a form via AJAX or JavaScript without changing the URL, the method remains POST even if the URL looks like a GET request. Relying solely on the presence of data in $_POST can lead to logic errors where valid state changes are ignored simply because the array is empty or not populated yet.

Handling Method Switching and Security

One of the primary reasons developers ask how to distinguish GET from POST is to implement method-specific routing or security controls. In Laravel, you can use route verbs (GET, POST) to define behavior at the router level. For example:

Route::get('/login', [UserController::class, 'showLoginForm']);
Route::post('/login', [UserController::class, 'handleLogin']);

However, sometimes you need to handle both methods within a single controller action, perhaps for AJAX-driven interfaces where the same endpoint accepts data via query parameters (GET) or form submissions (POST). In these cases, explicitly checking isMethod('POST') allows you to branch logic cleanly without relying on the presence of input data.

If your application requires strict separation, consider using Laravel's built-in middleware to validate that certain endpoints only accept specific methods. For instance, ensuring that a sensitive action is never reachable via GET prevents accidental data leakage or information disclosure vulnerabilities.

Why Avoid $_POST?

While if(!empty($_POST)) might seem like a quick check, it introduces several pitfalls. First, it is not type-safe and can break if the server configuration changes how form data is parsed. Second, it ignores the HTTP protocol standard, which defines methods based on the request line, not just the payload presence. Third, modern Laravel applications often use JSON APIs where $_POST is irrelevant for requests containing application/json content types; in those scenarios, you would check $request->json()->get('data') or simply rely on the method itself.

By shifting your focus to $request->isMethod('POST'), you align your codebase with Laravel's conventions, making it easier to refactor later and ensuring your application behaves consistently across different client types, from standard browsers to mobile apps and headless scripts. For more advanced request handling patterns or custom middleware requirements, resources like the official documentation or specialized guides from https://laravelcompany.com can provide deeper insights into building robust API controllers.

Our network