klein / klein.php

A fast & flexible router
MIT License
2.66k stars 290 forks source link

paramGet and paramPost #283

Open jk3us opened 9 years ago

jk3us commented 9 years ago

I feel like there should be a way to get a single value from either get or post explicitly. paramsGet() gives you the whole collection for get, and param() will give you a single value, but may not be the one you were looking for if there happens to be a get and post variable with the same name. So, right now, I'm just doing $post = $request->paramsPost()->all(); at the top and using $post just like $_POST, just somewhat less global.

If it could also somehow encapsulate/simplify php's filter_input* functionality, that could be pretty awesome. http://php.net/manual/en/function.filter-input-array.php

Rican7 commented 9 years ago

There really kind of is. :)

The $request->param() and $request->params() methods are just convenience methods that merge together all of the possible parameter locations. As you said, the paramsGet() method gives you the entire collection object, but then you still get the very convenient API. Calling $request->paramsGet()->all() returns a raw PHP array (not very friendly to use), which essentially defeats the purpose of the collections all together. Instead use the collections as they're given to you:

$params = $request->paramsPost();

$time_zone = new DateTimeZone($params->get('time_zone'));
$date = new DateTime($params->get('date'), $time_zone);