All web applications receive input via HTTP requests. The input can be sent to your application via any of the four HTTP verbs: GET, POST, PUT, or DELETE. Input can also be sent to your application via cookies, which can store small amounts of information and are stored on the user's computer.
Let's dig into the classes Laravel provides for working with user input!
Note: Laravel doesn't mess with your query strings. Feel free to use them.
The Input class handles input that comes into your application via GET, POST, PUT, or DELETE requests. Retrieving input using the Input class is effortless. Just use the get method:
$email = Input::get('email');
Note: The get method is used for all request types, not just GET requests. You may use it on POST, PUT, and DELETE requests as well.
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');
Now, "Fred" will be returned if the "name" input item does not exist. You may even pass a closure as a default value:
$name = Input::get('name', function() {return 'Fred';});
Need to determine if an input item exists? Use the has method:
if (Input::has('name'))
{
$name = Input::get('name');
}
Note: The has method will return false if the input item exists but is an empty string.
Need to access the $_FILES array? It's easy using the file method:
$picture = Input::file('picture');
$size = Input::file('picture.size');
Sometimes you may need to merge the input and $_FILES array. Check out the all method:
$input = Input::all();
Have you ever tried to re-populate an input form after an invalid form submission? It can get pretty clunky. Not in Laravel. You can easily retrieve the input from the previous request using the old method on the Input class:
$name = Input::old('name');
Note: You must specifiy a session driver before using the old Input method.
As you would expect, you may pass a default value in the second parameter to the method:
$name = Input::old('name', 'Fred');
Once again, there is a simple way to determine if an old input item exists using the had method:
if (Input::had('name'))
{
$name = Input::old('name');
}
The Cookie class provides simple functions for retrieving, setting, and deleting cookies.
To retrieve a cookie value, simply mention its name to the get method:
$name = Cookie::get('name');
Of course, just like the Input class, you may pass a default value in the second parameter to the get method:
$name = Cookie::get('name', 'Fred');
Also just like the Input class, the Cookie class has a simple method to determine if a cookie exists:
if (Cookie::has('name'))
{
$name = Cookie::get('name');
}
Need to create a cookie? No problem. Check out the put method:
Cookie::put('name', 'Fred', 60);
The put method accepts almost the exact same parameters as the PHP setcookie method. However, just pass the number of minutes you want the cookie to live as the third parameter. You don't have to worry about any clunky expiration date calculations.
If you need to create a "permanent" cookie, try the forever method. It creates a cookie that lives for five years:
Cookie::forever('name', 'Fred');
To delete a cookie, use the forget method:
Cookie::forget('name');
Almost every web application receives input through HTML forms. As you have probably already learned, Laravel is here to make your life easier. That's why generating forms using the Form class is a breeze.
Note: All input data displayed in elements generated by the Form class is filtered through the HTML::entities method.
Opening a form is simple. Just call the open method on the Form class:
echo Form::open();
When called without any parameters, the open method will create a form that will POST to the current URL. However, you'll probably want to point your forms to other URLs too. No problem. Just mention the URL to the method. You can even specify the request method (GET, POST, PUT, or DELETE) in the second parameter to the method:
echo Form::open('user/profile', 'PUT');
Need to apply a class or other attribute to the form tag generated by the open method? Simply pass an array of attributes as a third parameter:
echo Form::open('user/profile', 'PUT', array('class' => 'awesome'));
Note: The open method automatically prepares your form to receive UTF-8 input.
Need a form that can handle file uploads? Use the open_for_files method:
echo Form::open_for_files('user/profile');
Laravel provides an easy method of protecting your application from cross-site request forgeries. First, a random token is placed in your user's session. Don't sweat it, this is done automatically. Next, use the token method to generate a hidden form input field containing the random token on your form:
echo Form::token();
Now, simply attach the built-in CSRF filter to the route the form is posting to. If the token submitted by the form does not match the token in the user's session, the application/views/error/500.php view will be displayed.
Want to just get the CSRF token without generating a hidden input field? Use the raw_token method:
echo Form::raw_token();
Note: Don't forget to specify a session driver before using these methods.
Need to generate a label for a form element? It's simple using the label method. Just pass the label name and display value to the method:
echo Form::label('email', 'E-Mail Address');
Of course, you may pass any attributes you wish in the third parameter to the method:
echo Form::label('email', 'E-Mail Address', array('class' => 'awesome'));
Note: After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well.
Generating text boxes couldn't be easier. Just call the text method on the Form class and mention the name of the field:
echo Form::text('username');
Already have a value you want to put in the text box? Throw it in as a second parameter:
echo Form::text('email', 'example@gmail.com');
Again, any other attributes you wish to apply to the text box may be passed in an array as the third parameter:
echo Form::text('email', 'example@gmail.com', array('class' => 'awesome'));
Note: The password, hidden, and textarea methods have the same signature as the text method. You just learned four methods for the price of one!
What website doesn't have a checkbox? Actually, this one doesn't! But, thankfully, generating them is simple using the checkbox method on the Form class. Just give the checkbox a name and a value:
echo Form::checkbox('remember', 'yes');
Of course, the example above will generate the following HTML:
<input type="checkbox" name="remember" value="yes">
Need to generate a "checked" checkbox? No problem. Simply pass true as the third parameter to the method:
echo Form::checkbox('remember', 'yes', true);
As always, you may specify any extra attributes that should be applied to the checkbox. Pass them as the fourth parameter to the method:
echo Form::checkbox('remember', 'yes', true, array('class' => 'awesome'));
Note: The radio method has the same signature as the checkbox method. Two for one!
Generating drop-down lists can be a headache. Thankfully, Laravel makes it refreshingly simple using the select method on the Form class. All you need to do is give your list a name and an array of options:
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'));
If you wish to set the selected item, just pass the value as the third parameter to the method:
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');
You may specify any other attributes that should be applied to the list in the fourth parameter to the method:
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S', array('class' => 'awesome'));
Creating a submit button is a cinch. Use the submit method on the Form class:
echo Form::submit('Click Me!');
Again, any other attributes that should be applied to the button may be passed to the method:
echo Form::submit('Click Me!', array('class' => 'awesome'));
Note: Need to create a button element? Try the button method. It has the same signature as submit.