ReynierPM ReynierPM - 2 months ago 7
PHP Question

Check whether parameter exists or not in the request

I have this function:

/**
* @Secure(roles="IS_AUTHENTICATED_FULLY")
* @Route("/chequearFabricanteDistribuidor", name="chequearFabricanteDistribuidor", condition="request.headers.get('X-Requested-With') == 'XMLHttpRequest'")
* @Method("GET")
*/

public function chequearFabricanteAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:FabricanteDistribuidor')->findOneBy(
array( 'nombre' => $request->query->get('fabricante')['nombre'] )
);

$response['valid'] = true;

if ($entities) {
$response['valid'] = false;
}

return new JsonResponse($response);
}


The function needs to be called from two different forms and the only different is the request var that holds the value. In the first form is:
$request->query->get('fabricante')['nombre']
while in the second is
$request->query->get('distribuidor')['nombre']
so I'm asking if the right way to handle this could be:

if (isset($request->query->get('fabricante')))
{
$nombre = $request->query->get('fabricante')['nombre'];
}
else
{
$nombre = $request->query->get('distribuidor')['nombre'];
}


Is this right? Exists a better one?

Answer

As Cerad posted on the responses, you could use:

$request->query->has('distribuidor')
Comments