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:
$email = Input::get('email');
Note: The "get" method is used for all request types (GET, POST, PUT, and DELETE), not just GET requests.
$input = Input::get();
$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:
$name = Input::get('name', 'Fred');
$name = Input::get('name', function() {return 'Fred';});
if (Input::has('name')) ...
Note: The "has" method will return false if the input item is an empty string.
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:
$data = Input::json();
$files = Input::file();
$picture = Input::file('picture');
$size = Input::file('picture.size');
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:
Input::flash();
Input::flash('only', array('username', 'email'));
Input::flash('except', array('password', 'credit_card'));
$name = Input::old('name');
Note: You must specify a session driver before using the "old" method.
Further Reading:
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:
return Redirect::to('login')->with_input();
return Redirect::to('login')->with_input('only', array('username'));
return Redirect::to('login')->with_input('except', array('password'));
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.
$name = Cookie::get('name');
$name = Cookie::get('name', 'Fred');
Cookie::put('name', 'Fred', 60);
Cookie::forever('name', 'Fred');
Cookie::forget('name');
Sometimes you may wish to merge or replace the current input. Here's how:
Input::merge(array('name' => 'Spock'));
Input::replace(array('doctor' => 'Bones', 'captain' => 'Kirk'));
To clear all input data for the current request, you may use the clear
method:
Input::clear();