How do I obtain all the GET parameters on Silex?
In Request object you have access to multiple parameter bags, in particular:
-
$request->query
- the GET parameters -
$request->request
- the POST parameters -
$request->attributes
- the request attributes (includes parameters parsed from the PATH_INFO)
$request->query
contains GET parameters only. city_id
is not a GET parameter. It's an attribute parsed from the PATH_INFO.
Silex uses several Symfony Components. Request and Response classes are part of the HttpFoundation. Learn more about it from Symfony docs:
fesja
After finishing an Information & Technology Management Master in IIT, Chicago; I'm starting my own company.
Updated on May 19, 2020Comments
-
fesja almost 4 years
I've been using Silex for a day, and I have the first "stupid" question. If I have:
$app->get('/cities/{city_id}.json', function(Request $request, $city_id) use($app) { .... }) ->bind('city') ->middleware($checkHash);
I want to get all the parameters (city_id) included in the middleware:
$checkHash = function (Request $request) use ($app) { // not loading city_id, just the parameter after the ? $params = $request->query->all(); .... }
So, how do I get city_id (both the parameter name and its value) inside the middleware. I'm going to have like 30 actions, so I need something usable and maintainable.
What am I missing?
thanks a lot!
Solution
We need to get those extra parameters of $request->attributes
$checkHash = function (Request $request) use ($app) { // GET params $params = $request->query->all(); // Params which are on the PATH_INFO foreach ( $request->attributes as $key => $val ) { // on the attributes ParamaterBag there are other parameters // which start with a _parametername. We don't want them. if ( strpos($key, '_') != 0 ) { $params[ $key ] = $val; } } // now we have all the parameters of the url on $params ... });