Please note that this chapter was written for VERSION 3 of the Laravel PHP Framework.
Now that we know how to create forms, we need to learn how to handle the input that has been sent by them. As always, Laravel has provided an ultra clean way of dealing with your input data. No need to interact with PHP arrays such as $_POST
, $_GET
and $_FILES
. Let's be honest, those look ugly.
Request data
Let's use the Input class to handle this instead.
<?php
$panda = Input::get('panda');
Now we have a Panda! Great.. we have too many already. Due to an unfortunate welsh border breach, Wales is now infested with red pandas. They are everywhere, I'm using one as a footrest right now. How did you think Emma managed to snap up that cover shot, they come from China!
Anyway, let's get back on track. One thing that you need to remember about the get()
method on the Input
class is that it doesn't refer to $_GET
data. You will see that get()
is simply a nice and expressive short method name for retrieving all kinds of data. The Input
class also responds to get()
with all kinds of request data, including $_POST
.
If a piece of request data isn't set, the Input class will return a null
value. If you pass a second parameter to the get()
method and the index doesn't exist then the method will return the second parameter instead. This means that you can use it without worrying about any undefined
errors popping up. Very useful!
<?php
$panda = Input::get('panda', 'Muffin');
Now if our panda doesn't have a name it will be called 'Muffin', awwww..
If you would like to retrieve the entire request array, just skip the index. Easy as that.
<?php
$morepandas = Input::get();
By default the get()
array won't includes values from the $_FILES
array. However if you use all()
instead of get()
it will contain files too.
<?php
$pandas_and_files = Input::all();
If you would like to check if a piece of post data exists without actually returning it, simply use the elegant and highly expressive has()
method which will return a boolean result.
<?php
$do_we_have_a_panda = Input::has('panda');
Files
To access an element from the $_FILES
array, make a call to the Input::file()
method, for example..
<?php
$file = Input::file('spoon');
If you would like to retrieve a file attribute then add a period and an attribute key to the first parameter, for example to retrieve the file size..
$size = Input::file('spoon.size');
Once again, calling the method without a parameter will retrieve the full array of files.
* Note: You can use this syntax to access all multi dimensional arrays. Simply use a period to denote a nested array index. *
$files = Input::file();
Flash Data
Flash data is a useful method of storing data in the session for use in the next request. It can be a useful way to repopulate forms.
To flash
all of the current request data to the session, for it to be accessible in the next request, simply use the flash()
method.
<?php
Input::flash();
If you only want to flash a portion of the current request data, just pass 'only' as the first parameter to the method and an array of field names that you wish flashed
as the second parameter.
<?php
Input::flash('only', array('betty', 'simon'));
Now we will take Betty and Simon with us to the next request. Alternatively we could specify a list of fields that we don't want to take with us using the except
option, for example..
<?php
Input::flash('except', array('uncle_bob'));
There, now we can leave Uncle Bob behind, he's an arrogant soul, and dislikes our new national animal the red panda.
Now we can use the standard Redirect::to()
method to move to a new request. From here we can use the expressive Input::old()
method to retrieve a value that has been flashed
from a previous request.
<?php
$betty = Input::old('betty');
As you can see, Betty has survived the transition. You can think of flash data as those fuzzy transporter pads from Star Trek, moving Kirk and his buddies from one request to the next.
Once again you can skip the parameter to return a full array of flash data.
<?php
$people = Input::old();
You can use the had()
method to see if an index of flash data exists.
<?php
Input::had('uncle_bob');
Of course not, we hate Uncle Bob.
Laravel wouldn't be the framework it is without its wonderful short-cuts and expressive methods. Let's have a look at a prime example of this in action.
<?php
return Redirect::to('party')->with_input();
The with_input()
method will flash all of our request data for us, it also accepts the same only
and except
methods as our flash()
method.
<?php
return Redirect::to('party')->with_input('only', array('betty', 'simon'));
return Redirect::to('party')->with_input('except', array('uncle_bob'));
Now that you have access to form data, your applications will become much more interactive!
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!