Framework: Input::get() default value not working propertly

Created on 19 Nov 2013  路  5Comments  路  Source: laravel/framework

When you use Input::get('foo', 'bar') to get a default value for 'foo', it works good if the parameter doesnt exist.

But if you access for example /?foo=, the default value is ignored.

The function controlling this is value() (http://laravel.com/api/source-function-value.html#849-858)

I think there is something used here like isset() instead of empty(), but to me, an empty value should be considered empty and default should appear instead.

Most helpful comment

Just to add a workaround: Input::get('foo') ?: 'bar'

All 5 comments

Well, while talking in IRC about this, i noticed that this makes sense for querystring values. But for post values from a form, this situation would break things.

so after all i think this must be leave as it is. Or at least just fix it for querystrings where having an empty value is the same as having a null one.

Example that breaks the "Don't Repeat Yourself": example.com/?section=

$section = Input::get('section', 'index');
if ($section == '') {
    $section = 'index';
}

Well, while talking in IRC about this, i noticed that this makes sense for querystring values. But for post values from a form, this situation would break things.

It impossible to only cater $_GET since Symfony merged all of it when a request object is created.

$section = Input::get('section', 'index');

Above code are the same for $_GET and $_POST.

No intention to change this behavior.

Just to add a workaround: Input::get('foo') ?: 'bar'

Just to add a workaround: Input::get('foo') ?: 'bar'

It's weird but I am still getting this behavior in version 7.x.x. this workaround seems to fix it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriellimo picture gabriellimo  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments

YannPl picture YannPl  路  3Comments

ghost picture ghost  路  3Comments

felixsanz picture felixsanz  路  3Comments