Php 检查请求中是否存在参数
我有这个功能:Php 检查请求中是否存在参数,php,symfony,symfony-http-foundation,Php,Symfony,Symfony Http Foundation,我有这个功能: /** * @Secure(roles="IS_AUTHENTICATED_FULLY") * @Route("/chequearFabricanteDistribuidor", name="chequearFabricanteDistribuidor", condition="request.headers.get('X-Requested-With') == 'XMLHttpRequest'") * @Method("GET") */ public 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);
}
该函数需要从两种不同的形式调用,唯一不同的是保存该值的请求变量。第一种形式是:$request->query->get('fabricante')['nombre']
,而第二种形式是$request->query->get('distribuidor')['nombre']
,所以我想知道处理这个问题的正确方法是:
if (isset($request->query->get('fabricante')))
{
$nombre = $request->query->get('fabricante')['nombre'];
}
else
{
$nombre = $request->query->get('distribuidor')['nombre'];
}
是这样吗?有更好的吗?当Cerad发布在回复上时,您可以使用:
$request->query->has('distribuidor')
$request->query->has('distribuidor')对于那些在
$request->query->has(…)
返回null
的人,请确保您没有执行POST请求。对于POST请求,请使用$request->request->has(…)
。