/**
  * @ApiDoc()
  */
 public function postAction(ParamFetcher $paramFetcher, Request $request)
 {
     $row = $this->_model->createRow();
     $this->get('serializer')->denormalize($request->request->all(), get_class($row), 'json', array('object_to_populate' => $row, 'groups' => array('rest_write', 'rest')));
     $validator = $this->get('validator');
     $errors = $validator->validate($row);
     if (count($errors)) {
         $formattedErrors = array();
         foreach ($errors as $error) {
             $formattedErrors[] = array('message' => $error->getMessage(), 'code' => $error->getCode(), 'propertyPath' => $error->getPropertyPath());
         }
         $view = View::create(array('errors' => $formattedErrors), 400);
         $ctx = new Context();
         $view->setContext($ctx);
         return $view;
     } else {
         $this->denyAccessUnlessGranted('create', $row);
         $this->_beforeInsert($row);
         $this->_beforeSave($row);
         $row->save();
         $this->_afterSave($row, $request);
         $this->_afterInsert($row, $request);
         //there must be a better way to do that
         $getRouteName = preg_replace('#^post_(.*)$#', 'get_\\1', $request->get('_route'));
         return View::createRedirect($this->generateUrl($getRouteName, array('id' => $row->id, 'version' => $request->get('version'))), Codes::HTTP_CREATED);
     }
 }
Пример #2
0
 public function createViewRedirectCreated($id, Request $request, Router $router)
 {
     //there must be a better way to do that
     $getRouteName = preg_replace('#^post_(.*)$#', 'get_\\1', $request->get('_route'));
     return View::createRedirect($router->generate($getRouteName, array('id' => $id, 'version' => $request->get('version'))), Codes::HTTP_CREATED);
 }