如何在 Silex 上获取所有 GET 参数?
我已经使用Silex一天了,我有第一个“愚蠢”的问题。如果我有:
$app->get('/cities/{city_id}.json', function(Request $request, $city_id) use($app) {
....
})
->bind('city')
->middleware($checkHash);
我想获取中间件中包含的所有参数(city_id):
$checkHash = function (Request $request) use ($app) {
// not loading city_id, just the parameter after the ?
$params = $request->query->all();
....
}
那么,我如何在中间件中获取city_id(参数名称及其值)。我将有大约30个动作,所以我需要一些可用且可维护的东西。
我错过了什么?
多谢!
溶液
我们需要获得$request>属性的额外参数
$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
...
});