Requests & Input

Basic Input

You may access all user input with a few simple methods. 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.

Retrieving An Input Value

  1. $name = Input::get('name');

Retrieving A Default Value If The Input Value Is Absent

  1. $name = Input::get('name', 'Sally');

Determining If An Input Value Is Present

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

Getting All Input For The Request

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

Getting Only Some Of The Request Input

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

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

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

Note: Some JavaScript libraries such as Backbone may send input to the application as JSON. You may access this data via Input::get like normal.

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 = Cookie::get('name');
  1. $response = Response::make('Hello World');
  2. $response->withCookie(Cookie::make('name', 'value', $minutes));

If you would like to set a cookie before a response has been created, use the Cookie::queue() method. The cookie will automatically be attached to the final response from your application.

  1. Cookie::queue($name, $value, $minutes);
  1. $cookie = Cookie::forever('name', 'value');

Old Input

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

Flashing Input To The Session

  1. Input::flash();

Flashing Only Some Input To The Session

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

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::to('form')->withInput();
  2. return Redirect::to('form')->withInput(Input::except('password'));

Note: You may flash other data across requests using the Session class.

Retrieving Old Data

  1. Input::old('username');

Files

Retrieving An Uploaded File

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

Determining If A File Was Uploaded

  1. if (Input::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 (Input::file('photo')->isValid())
  2. {
  3. //
  4. }

Moving An Uploaded File

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

Retrieving The Path To An Uploaded File

  1. $path = Input::file('photo')->getRealPath();

Retrieving The Original Name Of An Uploaded File

  1. $name = Input::file('photo')->getClientOriginalName();

Retrieving The Extension Of An Uploaded File

  1. $extension = Input::file('photo')->getClientOriginalExtension();

Retrieving The Size Of An Uploaded File

  1. $size = Input::file('photo')->getSize();

Retrieving The MIME Type Of An Uploaded File

  1. $mime = Input::file('photo')->getMimeType();

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();

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 Request URL

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

Retrieve A Request URI Segment

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

Retrieving A Request Header

  1. $value = Request::header('Content-Type');

Retrieving Values From $_SERVER

  1. $value = Request::server('PATH_INFO');

Determining If The Request Is Over HTTPS

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

Determine If The Request Is Using AJAX

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

Determine If The Request Has JSON Content Type

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

Determine If The Request Is Asking For JSON

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

Checking The Requested Response Format

The Request::format method will return the requested response format based on the HTTP Accept header:

  1. if (Request::format() == 'json')
  2. {
  3. //
  4. }