0

Request method detection, path, URL, headers

Beginner5 min read·lv-09-003

Concept

The Request object provides rich methods for inspecting the request's method, path, URL, and headers — essential for conditional logic in middleware and controllers.

Method detection:

  • $request->method(): Returns the HTTP method string: "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD".
  • $request->isMethod('post'): Case-insensitive method check.
  • $request->isReadVerb(): True for GET, HEAD, OPTIONS.

Path and URL:

  • $request->path(): Path without leading slash: api/users/42.
  • $request->decodedPath(): URL-decoded path.
  • $request->is(string ...$patterns): True if the path matches any pattern. Supports * wildcards: $request->is('api/*').
  • $request->routeIs(string ...$patterns): Matches current route NAME, not path.
  • $request->url(): Full URL without query string: https://app.com/api/users/42.
  • $request->fullUrl(): Full URL with query string.
  • $request->fullUrlWithQuery(array $query): Full URL replacing/adding query params.
  • $request->fullUrlWithoutQuery(array $keys): Full URL removing specific query params.

Content type and expected response format:

  • $request->expectsJson(): True if Accept: application/json or XMLHttpRequest with no specific HTML accept.
  • $request->wantsJson(): Alias for expectsJson() in some contexts.
  • $request->acceptsJson(): True if Accept header includes application/json.
  • $request->acceptsHtml(): True if Accept header includes text/html.

Code Example

php
<?php
use Illuminate\Http\Request;

// Method detection
$request->method();            // "POST"
$request->isMethod('get');     // false
$request->isMethod('post');    // true
$request->isMethod(['get', 'post']); // check multiple

// Path matching (no leading /)
$request->path();   // "admin/users/42"
$request->is('admin/*');   // true — wildcard match
$request->is('api/v1/*', 'api/v2/*'); // true if matches either
$request->is('dashboard');  // false — exact match required

// URL components
$request->url();      // "https://app.com/admin/users/42"
$request->fullUrl();  // "https://app.com/admin/users/42?page=2&sort=name"
$request->fullUrlWithQuery(['page' => 3]); // "https://app.com/admin/users/42?page=3&sort=name"
$request->fullUrlWithoutQuery(['page']);   // "https://app.com/admin/users/42?sort=name"

// Route name matching (useful in middleware)
$request->routeIs('admin.*');          // true for any admin.* named route
$request->routeIs('login', 'register'); // true if current route name is login OR register

// Content negotiation
$request->expectsJson(); // true if client wants JSON response
// Useful for returning JSON vs HTML:
if ($request->expectsJson()) {
    return response()->json(['error' => 'Not found'], 404);
}
return view('errors.404');

// Request fingerprint — unique string for current request (route + IP + user agent)
$fingerprint = $request->fingerprint(); // unique hash per request signature

// Headers
$request->header('X-Request-Id');
$request->hasHeader('Authorization');
$request->headers->has('X-Custom');
$request->headers->all(); // all headers as array