input.md 4.73 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5
# Input & Cookies

## Contents

- [Input](#input)
Taylor Otwell committed
6
- [JSON Input](#json)
Taylor Otwell committed
7 8 9 10
- [Files](#files)
- [Old Input](#old-input)
- [Redirecting With Old Input](#redirecting-with-old-input)
- [Cookies](#cookies)
11
- [Merging & Replacing](#merge)
Taylor Otwell committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

<a name="input"></a>
## Input

The **Input** class handles input that comes into your application via GET, POST, PUT, or DELETE requests. Here are some examples of how to access input data using the Input class:

#### Retrieve a value from the input array:

	$email = Input::get('email');

> **Note:** The "get" method is used for all request types (GET, POST, PUT, and DELETE), not just GET requests.

#### Retrieve all input from the input array:

	$input = Input::get();

#### Retrieve all input including the $_FILES array:

	$input = Input::all();

By default, *null* will be returned if the input item does not exist. However, you may pass a different default value as a second parameter to the method:

#### Returning a default value if the requested input item doesn't exist:

	$name = Input::get('name', 'Fred');

#### Using a Closure to return a default value:

	$name = Input::get('name', function() {return 'Fred';});

#### Determining if the input contains a given item:

	if (Input::has('name')) ...

> **Note:** The "has" method will return *false* if the input item is an empty string.

Taylor Otwell committed
48 49 50 51 52 53 54 55 56
<a name="json"></a>
## JSON Input

When working with JavaScript MVC frameworks like Backbone.js, you will need to get the JSON posted by the application. To make your life easier, we've included the `Input::json` method:

#### Get JSON input to the application:

	$data = Input::json();

Taylor Otwell committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
<a name="files"></a>
## Files

#### Retrieving all items from the $_FILES array:

	$files = Input::file();

#### Retrieving an item from the $_FILES array:

	$picture = Input::file('picture');

#### Retrieving a specific item from a $_FILES array:

	$size = Input::file('picture.size');

72 73 74 75
> **Note:** In order to use file uploads, you must use `Form::open_for_files()` or manually enable `multipart/form-data`.

*Further Reading:*

Vinícius Fragoso committed
76
- *[Opening Forms](/docs/views/forms#opening-a-form)*
77

Taylor Otwell committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
<a name="old-input"></a>
## Old Input

You'll commonly need to re-populate forms after invalid form submissions. Laravel's Input class was designed with this problem in mind. Here's an example of how you can easily retrieve the input from the previous request. First, you need to flash the input data to the session:

#### Flashing input to the session:

	Input::flash();

#### Flashing selected input to the session:

	Input::flash('only', array('username', 'email'));

	Input::flash('except', array('password', 'credit_card'));

#### Retrieving a flashed input item from the previous request:

	$name = Input::old('name');

> **Note:** You must specify a session driver before using the "old" method.

*Further Reading:*

- *[Sessions](/docs/session/config)*

<a name="redirecting-with-old-input"></a>
## Redirecting With Old Input

Now that you know how to flash input to the session. Here's a shortcut that you can use when redirecting that prevents you from having to micro-manage your old input in that way:

#### Flashing input from a Redirect instance:

	return Redirect::to('login')->with_input();

#### Flashing selected input from a Redirect instance:

	return Redirect::to('login')->with_input('only', array('username'));

	return Redirect::to('login')->with_input('except', array('password'));

<a name="cookies"></a>
## Cookies

Laravel provides a nice wrapper around the $_COOKIE array. However, there are a few things you should be aware of before using it. First, all Laravel cookies contain a "signature hash". This allows the framework to verify that the cookie has not been modified on the client. Secondly, when setting cookies, the cookies are not immediately sent to the browser, but are pooled until the end of the request and then sent together. This means that you will not be able to both set a cookie and retrieve the value that you set in the same request.

#### Retrieving a cookie value:

	$name = Cookie::get('name');

#### Returning a default value if the requested cookie doesn't exist:

	$name = Cookie::get('name', 'Fred');

#### Setting a cookie that lasts for 60 minutes:

	Cookie::put('name', 'Fred', 60);

#### Creating a "permanent" cookie that lasts five years:

	Cookie::forever('name', 'Fred');

#### Deleting a cookie:

141 142 143 144 145 146 147 148 149 150 151 152 153
	Cookie::forget('name');

<a name="merge"></a>
## Merging & Replacing

Sometimes you may wish to merge or replace the current input. Here's how:

#### Merging new data into the current input:

	Input::merge(array('name' => 'Spock'));

#### Replacing the entire input array with new data:

154
	Input::replace(array('doctor' => 'Bones', 'captain' => 'Kirk'));
155 156 157

## Clearing Input

Taylor Otwell committed
158
To clear all input data for the current request, you may use the `clear` method:
159 160

	Input::clear();