HTTP Requests

    To obtain an instance of the current HTTP request via dependency injection, you should type-hint the class on your controller constructor or method. The current request instance will automatically be injected by the :

    If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies. For example, if your route is defined like so:

    1. Route::put('user/{id}', '[email protected]');

    You may still type-hint the Illuminate\Http\Request and access your route parameter id by defining your controller method like the following:

    1. <?php
    2. namespace App\Http\Controllers;
    3. use Illuminate\Http\Request;
    4. class UserController extends Controller
    5. {
    6. /**
    7. * Update the specified user.
    8. *
    9. * @param Request $request
    10. * @param string $id
    11. * @return Response
    12. */
    13. public function update(Request $request, $id)
    14. {
    15. //
    16. }
    17. }

    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. Here are a few more of the useful methods available on this class:

    Retrieving The Request URI

    The path method returns the request's URI. So, if the incoming request is targeted at , the path method will return foo/bar:

    1. $uri = $request->path();

    The is method allows you to verify that the incoming request URI matches a given pattern. You may use the * character as a wildcard when utilizing this method:

    1. if ($request->is('admin/*')) {
    2. //
    3. }

    To get the full URL, not just the path info, you may use the url or fullUrl methods on the request instance:

    1. // Without Query String...
    2. $url = $request->url();
    3. // With Query String...
    4. $url = $request->fullUrl();

    You may also get the full URL and append query parameters. For example, if the request is targeted at http://domain.com/foo, the following method will return :

    1. $url = $request->fullUrlWithQuery(['bar' => 'baz']);

    Retrieving The Request Method

    The method method will return the HTTP verb for the request. You may also use the isMethod method to verify that the HTTP verb matches a given string:

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

    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, 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:

    1. composer require symfony/psr-http-message-bridge
    2. composer require zendframework/zend-diactoros

    Once you have installed these libraries, you may obtain a PSR-7 request by simply type-hinting the request type on your route or controller:

    1. use Psr\Http\Message\ServerRequestInterface;
    2. Route::get('/', function (ServerRequestInterface $request) {
    3. //
    4. });

    Retrieving Input

    Retrieving An Input Value

    Using a few simple methods, you may access all user input from your Illuminate\Http\Request instance. You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs:

    You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:

      When working on forms with array inputs, you may use "dot" notation to access the arrays:

      1. $name = $request->input('products.0.name');
      2. $names = $request->input('products.*.name');

      Retrieving JSON Input Values

      When sending JSON requests to your application, you may access the JSON data via the input method as long as the Content-Type header of the request is properly set to application/json. You may even use "dot" syntax to dig deeper into JSON arrays:

      1. $name = $request->input('user.name');

      Determining If An Input Value Is Present

      To determine if a value is present on the request, you may use the has method. The has method returns true if the value is present and is not an empty string:

      1. if ($request->has('name')) {
      2. //
      3. }

      Retrieving All Input Data

      You may also retrieve all of the input data as an array using the all method:

      1. $input = $request->all();

      Retrieving A Portion Of The Input Data

      If you need to retrieve a sub-set of the input data, you may use the only and except methods. Both of these methods will accept a single array or a dynamic list of arguments:

      1. $input = $request->only(['username', 'password']);
      2. $input = $request->only('username', 'password');
      3. $input = $request->except(['credit_card']);
      4. $input = $request->except('credit_card');

      Dynamic Properties

      You may also access user input using dynamic properties on the Illuminate\Http\Request instance. For example, if one of your application's forms contains a name field, you may access the value of the posted field like so:

      1. $name = $request->name;

      When using dynamic properties, Laravel will first look for the parameter's value in the request payload and then in the route parameters.

      Laravel allows you to keep input from one request during the next request. This feature is particularly useful for re-populating forms after detecting validation errors. However, if you are using Laravel's included , it is unlikely you will need to manually use these methods, as some of Laravel's built-in validation facilities will call them automatically.

      Flashing Input To The Session

      The flash method on the Illuminate\Http\Request instance will flash the current input to the so that it is available during the user's next request to the application:

      1. $request->flash();
      1. $request->flashOnly(['username', 'email']);
      2. $request->flashExcept('password');

      Flash Input Into Session Then Redirect

      Since you often will want to flash input in association with a redirect to the previous page, you may easily chain input flashing onto a redirect using the withInput method:

      Retrieving Old Data

      To retrieve flashed input from the previous request, use the old method on the Request instance. The old method provides a convenient helper for pulling the flashed input data out of the session:

      1. $username = $request->old('username');

      Laravel also provides a global old helper function. If you are displaying old input within a , it is more convenient to use the old helper. If no old input exists for the given string, null will be returned:

      1. <input type="text" name="username" value="{{ old('username') }}">

      Retrieving Cookies From The Request

      All cookies created by the Laravel framework are encrypted and signed with an authentication code, meaning they will be considered invalid if they have been changed by the client. To retrieve a cookie value from the request, you may use the cookie method on the Illuminate\Http\Request instance:

      1. $value = $request->cookie('name');

      Laravel provides a global cookie helper function which serves as a simple factory for generating new Symfony\Component\HttpFoundation\Cookie instances. The cookies may be attached to a Illuminate\Http\Response instance using the withCookie method:

      1. $response = new Illuminate\Http\Response('Hello World');
      2. $response->withCookie('name', 'value', $minutes);
      3. return $response;

      To create a long-lived cookie, which lasts for five years, you may use the forever method on the cookie factory by first calling the cookie helper with no arguments, and then chaining the forever method onto the returned cookie factory:

      1. $response->withCookie(cookie()->forever('name', 'value'));

      Retrieving Uploaded Files

      You may access uploaded files that are included with the Illuminate\Http\Request instance using the file method. The object returned by the file method is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile class, which extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file:

      1. $file = $request->file('photo');

      You may determine if a file is present on the request using the hasFile method:

      1. if ($request->hasFile('photo')) {
      2. //
      3. }

      Validating Successful Uploads

      In addition to checking if the file is present, you may verify that there were no problems uploading the file via the isValid method:

      1. if ($request->file('photo')->isValid()) {
      2. //
      3. }

      Moving Uploaded Files

      To move the uploaded file to a new location, you should use the move method. This method will move the file from its temporary upload location (as determined by your PHP configuration) to a more permanent destination of your choosing:

      1. $request->file('photo')->move($destinationPath);
      2. $request->file('photo')->move($destinationPath, $fileName);

      Other File Methods

      There are a variety of other methods available on instances. Check out the API documentation for the class for more information regarding these methods.