HTTP Requests
Introduction
Laravel's Illuminate\Http\Request
class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.
Interacting With The Request
Accessing The Request
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request
class on your route closure or controller method. The incoming request instance will automatically be injected by the Laravel service container:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
As mentioned, you may also type-hint the Illuminate\Http\Request
class on a route closure. The service container will automatically inject the incoming request into the closure when it is executed:
use Illuminate\Http\Request;
Route::get('/', function (Request $request) {
//
});
Dependency Injection & Route Parameters
If your controller method is also expecting input from a route parameter you should list your route parameters after your other dependencies. For example, if your route is defined like so:
use App\Http\Controllers\UserController;
Route::put('/user/{id}', [UserController::class, 'update']);
You may still type-hint the Illuminate\Http\Request
and access your id
route parameter by defining your controller method as follows:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Update the specified user.
*
* @param \Illuminate\Http\Request $request
* @param string $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
}
Request Path & Method
The Illuminate\Http\Request
instance provides a variety of methods for examining the incoming HTTP request and extends the Symfony\Component\HttpFoundation\Request
class. We will discuss a few of the most important methods below.
Retrieving The Request Path
The path
method returns the request's path information. So, if the incoming request is targeted at http://example.com/foo/bar
, the path
method will return foo/bar
:
$uri = $request->path();
Inspecting The Request Path / Route
The is
method allows you to verify that the incoming request path matches a given pattern. You may use the *
character as a wildcard when utilizing this method:
if ($request->is('admin/*')) {
//
}
Using the routeIs
method, you may determine if the incoming request has matched a named route:
if ($request->routeIs('admin.*')) {
//
}
Retrieving The Request URL
To retrieve the full URL for the incoming request you may use the url
or fullUrl
methods. The url
method will return the URL without the query string, while the fullUrl
method includes the query string:
$url = $request->url();
$urlWithQueryString = $request->fullUrl();
If you would like to append query string data to the current URL, you may call the fullUrlWithQuery
method. This method merges the given array of query string variables with the current query string:
$request->fullUrlWithQuery(['type' => 'phone']);