Last Updated: February 25, 2016
·
3.491K
· raphaelstolt

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.