Lower case all request query parameter keys in a Silex application
To avoid case sensitive query parameter names/keys, which might lead to misses when pulling data via $request->get('fooBar')
, the following transformation placed in a Silex middleware comes in handy.
<?php
use Symfony\Component\HttpFoundation\Request;
$app->before(function (Request $request) {
$request->query->replace(
array_change_key_case($request->query->all())
);
});
Afterwards the query parameter values can be pulled, without a miss, via $request->get('foobar')
even when the request has been made via ?fooBar=baz
or even ?FooBar=baz
.
Written by Raphael Stolt
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#