HTTP Requests

Obtaining A Request Instance

Via Facade

The Request facade will grant you access to the current request that is bound in the container. For example:

  1. $name = Request::input('name');

Remember, if you are in a namespace, you will have to import the Request facade using a use Request; statement at the top of your class file.

Via Dependency Injection

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 service container:

  1. <?php namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use Illuminate\Routing\Controller;
  4. class UserController extends Controller {
  5. /**
  6. * Store a new user.
  7. *
  8. * @param Request $request
  9. * @return Response
  10. */
  11. public function store(Request $request)
  12. {
  13. $name = $request->input('name');
  14. //
  15. }
  16. }

If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies:

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

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.

  1. $name = Request::input('name');

Retrieving A Default Value If The Input Value Is Absent

  1. $name = Request::input('name', 'Sally');

Determining If An Input Value Is Present

  1. if (Request::has('name'))
  2. {
  3. //
  4. }

Getting All Input For The Request

  1. $input = Request::all();

Getting Only Some Of The Request Input

  1. $input = Request::only('username', 'password');
  2. $input = Request::except('credit_card');

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

  1. $input = Request::input('products.0.name');

Old Input

Laravel also allows you to keep input from one request during the next request. For example, you may need to re-populate a form after checking it for validation errors.

Flashing Input To The Session

The flash method will flash the current input to the session so that it is available during the user's next request to the application:

  1. Request::flash();

Flashing Only Some Input To The Session

  1. Request::flashOnly('username', 'email');
  2. Request::flashExcept('password');

Flash & 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.

  1. return redirect('form')->withInput();
  2. return redirect('form')->withInput(Request::except('password'));

Retrieving Old Data

To retrieve flashed input from the previous request, use the old method on the Request instance.

  1. $username = Request::old('username');

If you are displaying old input within a Blade template, it is more convenient to use the old helper:

  1. {{ old('username') }}

Cookies

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.

  1. $value = Request::cookie('name');

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

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

By "forever", we really mean five years.

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

Queueing Cookies

You may also "queue" a cookie to be added to the outgoing response, even before that response has been created:

  1. <?php namespace App\Http\Controllers;
  2. use Cookie;
  3. use Illuminate\Routing\Controller;
  4. class UserController extends Controller
  5. {
  6. /**
  7. * Update a resource
  8. *
  9. * @return Response
  10. */
  11. public function update()
  12. {
  13. Cookie::queue('name', 'value');
  14. return response('Hello World');
  15. }
  16. }

Files

Retrieving An Uploaded File

  1. $file = Request::file('photo');

Determining If A File Was Uploaded

  1. if (Request::hasFile('photo'))
  2. {
  3. //
  4. }

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.

Determining If An Uploaded File Is Valid

  1. if (Request::file('photo')->isValid())
  2. {
  3. //
  4. }

Moving An Uploaded File

  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 UploadedFile instances. Check out the API documentation for the class for more information regarding these methods.

Other Request Information

The Request class provides many methods for examining the HTTP request for your application and extends the Symfony\Component\HttpFoundation\Request class. Here are some of the highlights.

Retrieving The Request URI

  1. $uri = Request::path();

Determine If The Request Is Using AJAX

  1. if (Request::ajax())
  2. {
  3. //
  4. }

Retrieving The Request Method

  1. $method = Request::method();
  2. if (Request::isMethod('post'))
  3. {
  4. //
  5. }

Determining If The Request Path Matches A Pattern

  1. if (Request::is('admin/*'))
  2. {
  3. //
  4. }

Get The Current Request URL

  1. $url = Request::url();