Reputation: 1323
is there a better, or more efficient way to call a method from the variable of the route:
$app->get('/ac/{route}', function (Request $request, $route) use ($app) {
switch ($route) {
case 'formejur':
$infos = $app['manager.ac']->getPropFormejur( $request->get('find') );
break;
case 'naf':
$infos = $app['manager.ac']->getPropNaf( $request->get('find') );
break;
case 'fonction':
$infos = $app['manager.ac']->getPropFonction( $request->get('find') );
break;
case 'ville':
$infos = $app['manager.ac']->getPropVille( $request->get('find') );
break;
case 'dep':
$infos = $app['manager.ac']->getPropDepartement( $request->get('find') );
break;
default: break;
}
$response = new JsonResponse();
$response->setData($infos);
return $response->getContent();
});
It is only the method of $app['manager.ac'] who change.
Upvotes: 0
Views: 93
Reputation: 5679
You can use call_user_func_array
function for it:
$map = [
'formejur' => 'getPropFormejur',
'naf' => 'getPropNaf',
...
];
if (!empty($map[$route])) {
$infos = call_user_func_array(
[$app['manager.ac'], $map[$route]],
[$request->get('find')]
);
}
Upvotes: 3