xc2gqq
Last Updated: February 25, 2016
·
2.647K
· raphaelstolt
Tweet avatar corner

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.

Say Thanks
Respond