HTTP Requests
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 controller method. The incoming request instance will automatically be injected by the service container:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
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:
Route::put('user/{id}', 'UserController@update');
You may still type-hint the Illuminate\Http\Request
and access your route parameter id
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 Request $request
* @param string $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
}
Accessing The Request Via Route Closures
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) {
//
});
Request Path & Method
The Illuminate\Http\Request
instance provides a variety of methods for examining the HTTP request for your application 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://domain.com/foo/bar
, the path
method will return foo/bar
:
$uri = $request->path();
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/*')) {
//
}
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:
// Without Query String...
$url = $request->url();
// With Query String...
$url = $request->fullUrl();
Retrieving The Request Method
The method
method will return the HTTP verb for the request. You may use the isMethod
method to verify that the HTTP verb matches a given string:
$method = $request->method();
if ($request->isMethod('post')) {
//
}
PSR-7 Requests
The PSR-7 standard specifies interfaces for HTTP messages, including requests and responses. If you would like to obtain an instance of a PSR-7 request instead of a Laravel request, you will first need to install a few libraries. Laravel uses the Symfony HTTP Message Bridge component to convert typical Laravel requests and responses into PSR-7 compatible implementations:
composer require symfony/psr-http-message-bridge
composer require nyholm/psr7
Once you have installed these libraries, you may obtain a PSR-7 request by type-hinting the request interface on your route Closure or controller method:
use Psr\Http\Message\ServerRequestInterface;
Route::get('/', function (ServerRequestInterface $request) {
//
});
If you return a PSR-7 response instance from a route or controller, it will automatically be converted back to a Laravel response instance and be displayed by the framework.
Input Trimming & Normalization
By default, Laravel includes the TrimStrings
and ConvertEmptyStringsToNull
middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel
class. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null
. This allows you to not have to worry about these normalization concerns in your routes and controllers.
If you would like to disable this behavior, you may remove the two middleware from your application's middleware stack by removing them from the $middleware
property of your App\Http\Kernel
class.
Retrieving Input
Retrieving All Input Data
You may also retrieve all of the input data as an array
using the all
method:
$input = $request->all();
Retrieving An Input Value
Using a few simple methods, you may access all of the user input from your Illuminate\Http\Request
instance without worrying about which HTTP verb was used for the request. Regardless of the HTTP verb, the input
method may be used to retrieve user input:
$name = $request->input('name');