이 코드가 있는데 코드 실행을 중지하고 실패하면 직접 물마루 응답으로 이동하는 가장 좋은 방법은 무엇입니까? 코드 실행을 중지하고 catch (\Exception $ex)
코드의 실행을 중지해야하는 경우 JsonResponse
오류가 발생하면 중지하고 Json 응답을 반환하십시오.
JsonResponse
- 만약
!$productoSolicitud
경우 :이 경우public function guardarPaso6Action(Request $request) { $em = $this->getDoctrine()->getManager(); $session = $request->getSession(); $response['success'] = false; $status = null; if ($request->isXmlHttpRequest()) { $productoSolicitud = $session->get('productoSolicitudId'); $productoSolicitudEntity = $em->getRepository("AppBundle:ProductoSolicitud")->find($productoSolicitud); if (! $productoSolicitud) { $status = 400; $response['error'] = $this->get('translator')->trans('mensajes.msgElementoNoEncontrado', array('%pronombre%' => 'la','%elemento%' => 'solicitud de producto'), 'AppBundle'); } $fabricanteEntity = new Entity\FabricanteDistribuidor(); $fabricanteForm = $this->createForm(new Form\FabricanteForm(), $fabricanteEntity); $fabricanteForm->handleRequest($request); if ($fabricanteForm->isValid()) { try { $em->persist($fabricanteEntity); $em->flush(); $session->set('fabricanteEntityId', $fabricanteEntity->getId()); } catch (\Exception $ex) { $status = 400; $response['error'] = $ex->getMessage(); } // Some code goes here $dataResponse['paises'] = $nombresPaises; $response['entities'] = $dataResponse; $response['success'] = true; } else { $status = 400; $response['error'] = $this->getFormErrors($fabricanteForm); } } return new JsonResponse($response, $status ?: 200); }
하십시오 이것 봐
큰 아이디어를 얻었습니까? 오류가 발생하면 코드 실행을 중지하고 return
문을 사용합니다. 나는 이것을 어떻게 얻는가? exit
또는 break
을 사용하면 나에게 의심의 여지가 있지만이 문제에 대한 조언이 있는지 확실하지 않습니다.