Check if parameter exists in 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 must be called from two different forms, and the only difference is the var request, which contains the value. In the first form it is: $request->query->get('fabricante')['nombre'] , and in the second it is $request->query->get('distribuidor')['nombre'] , so I ask is there a proper way to handle this:

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

Is it correct? Is there a better one?

+5
source share
1 answer

As Cerad posted the answers, you can use:

 $request->query->has('distribuidor') 
+7
source

Source: https://habr.com/ru/post/1214523/


All Articles