What is the best practices for request a number of parameters in API URL and request a specific type for that.
For example I have the url: http://host/my_api/?param_1=p1¶m_2=p2
Now I have in parameters.yml a parameter that say how many parameters I need to have in URL and in method I check if the parameters have a specific type that I need.
But I'm not sure if that is the best practice for request parameters. Is useful for me to know if a parameter is missing what is that for a specific error message.
Any advice or opinion? Thanks!
*I use FOSRestBundle for create an API URL.
if you want to stick to query-parameters you could add a simple "checkFunction", for example if you want your two parameters to be required you could do sth as simple like this in your controller
/**
*@View
*/
public function apiAction(Request $request)
{
$this->checkRequiredParams($request);
...
}
private function checkRequiredParams($request){
$required = ["param_1","param_2"];
foreach ($required as $r) {
if(!$request->get($r)){
throw new AccessDeniedException("ACCESS DENIED - missing parameter $r ");
}
}
return true;
}
so it throws an exception if a parameter is missing, in this function you could also test for pattern or typecheck the value dont forget to
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
if you want to make use of it