Backend Development 5 min read

Handling HTTP Requests and Input Retrieval in Laravel

This article explains how Laravel simplifies accessing request data by using the Illuminate\Http\Request instance, automatic controller injection, and various helper methods to obtain request methods, URLs, and input values, including full, partial, and boolean inputs.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Handling HTTP Requests and Input Retrieval in Laravel

Laravel makes retrieving request input convenient, offering multiple methods.

Using the Illuminate\Http\Request instance, you can access request data directly.

use Illuminate\Http\Request;
$request = new Request();
echo $request->method();

Alternatively, you can use automatic injection in a controller method.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

class IndexController extends Controller
{
    public function index(Request $request)
    {
        dump($request->method());
    }
}

To get the request method, call method() ; to check a specific method, use isMethod('post') .

if ($request->isMethod('post')) {
    // ...
}

Request path can be obtained via path() , url() , and fullUrl() , each returning different parts of the URL.

path(): // returns "path1/path2"
url(): // returns "http://example.com/path1/path2"
fullUrl(): // returns "http://example.com/path1/path2?a=23&b=3"

To retrieve input, you can use all() or input() for all data, input('name') for a specific field, optionally providing a default value.

$request->all();
$request->input('name');
$request->input('name', 'php.cn');

Nested input can be accessed using dot notation, e.g., input('users.name') , input('users.0.name') , or input('users.*.name') .

$request->input('users.name');
$request->input('users.0.name');
$request->input('users.*.name');

The query() method works like input() but only reads from the query string.

Dynamic property access allows retrieving input as $request->name , with Laravel checking the request body first, then route parameters.

To obtain a boolean value, use boolean('name') .

if ($request->boolean('name')) {
    // ...
}

To check existence, use has() (true if field exists) or filled() (true if field exists and is not empty). has() can check multiple fields, while hasAny() returns true if any of the given fields exist.

if ($request->has(['name', 'email'])) {}
if ($request->hasAny(['name', 'email'])) {}

To retrieve a subset of input, use only() or except() , which return an associative array of the selected fields.

$request->only('name', 'email');
// or
$request->only(['name', 'email']);
Backend DevelopmentInput HandlingHTTP request
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.