Laravel 4: Getting User Input

5 min read

No matter what application you're developing, or what platform you are developing on, it is a safe bet you need to get input from your users some how. Laravel 4 provides a really convenient way to do this using the Input facade. Let's look at a simple form in Laravel:


    <input type="text" name="firstName">
    <input type="text" name="lastName">
    <input type="submit" value="Create me!">

Our form has two fields: a firstName and a lastName. They will be used to enter our user's first and last name (crazy). For the sake of simplicity, we will use a route to handle the action of our form instead of a controller (you really should be using controllers for stuff like this):

<?php

Route::post('user/create', function()
{
    // Handle the user create form.
});

Getting an Input Element's Value

Inside of our route, we can use the get method on the Input facade to get our input, and it really is as easy as doing this:

<?php

Route::post('user/create', function()
{
    // Get the user's first and last name.
    $firstName = Input::get('firstName');
    $lastName  = Input::get('lastName');
});

Specifying a Default Value

When we use the get() method, we pass the name of the field we want the value of as the first argument. We can optionally use a second argument to specify a default value if one has not been set:

<?php

Route::post('user/create', function()
{
    // Get the user's first and last name.
    $firstName = Input::get('firstName', 'John');
    $lastName  = Input::get('lastName', 'Doe');
});

What the above code will do is first look to see if the user has entered their first and last name. If they did not enter a first name our $firstName variable will contain the value John. Likewise if they didn't supply a last name the $lastName variable would be set to Doe.

We can also get all of the input values by using the all() method. This will return an associative array where the keys are the field name and their associated user input.

<?php

Route::post('user/create', function()
{
    // Get the user input as an array.
    $input = Input::all();

    // Just dump the array to the page.
    var_dump($input);
});

When this code runs, we will see something similar to this on the page:

array (size=3)
  '_token' => string 'vhT6UIOCVALts10RiSMtqYNO1UyjyHf7OfV8uGlH' (length=40)
  'firstName' => string 'John' (length=4)
  'lastName' => string 'Doe' (length=3)

I'm Getting The Wrong Data?

So we know how to get the input from the user, but how do we handle the occasional situation where we have ambiguous input?

For example say we have a form field named name set to John Doe for a user's name using a POST request. In addition we also have a URL query parameter with the same name set to accounting for some other purpose. If we use the following code what value would we get?

<?php

Route::post('user/create', function()
{
    $name = Input::get('name');
});

We would get the user's name, and not the accounting value. This is because when we use the get() function, data from a POST request have higher precedence over query parameters. To get around this issue we can specify the source of the data we want.

First we are going to have to work with an actual instance of the input object instead of using the Input facade. We have to do this in order to access the instance's properties.

<?php

Route::post('user/create', function()
{

    $requestData = Input::instance();

});

The instance() method returns the current request instance. When we store a reference to this in a variable (instead of using the Input facade) we can access its public properties. Each request object has a query and a request public property.

Tip: The Input facade and the Request facade provide access to the same object instance. This means that you can also use Request::get() for your input. However, you should use whichever facade makes sense for your current task for optimum readability.

This next part should look familiar to user's coming from Symphony 2. We can access the following public properties and use their get() method:

<?php

Route::post('user/create', function()
{

    $requestData = Input::instance();

    $userName = $requestData->request->get('name');

    $queryName = $requestData->query->get('name');

    var_dump($userName, $queryName);

});

When the above code runs we should see something like this in the browser:

string 'asdf' (length=4)
string 'accountant' (length=10)

The request property contains our POST data and the query property contains our URL query data (also known as GET data). Both properties have a get() method that works the same way as Input::get() except that we are explicitly saying we want POST data or that we specifically want GET data.

Comments

There are no comments. Be the first to comment!

Up next