php / symfony:检索POST / PUT api

时间:2017-06-19 12:08:12

标签: php symfony

对于我在页面中使用的数据表(webix datatable),我必须使用REST API。

我的网址例如:http://localhost:8000/trial/1

在此页面中进行api调用我使用以下内容:

save: "rest->{{ path('api_i_post') }}",
url: "rest->{{ path('erp_interventionapi_get', { trialid: trial.id })

使用GET方法,我检索试验(/ trial / 1),从数据库加载并填充数据表的许多干预。

使用这个数据表,我可以“添加一个新行”。它使用POST方法(保存:“rest-> {{path('api_i_post')}}”)

当我添加新行时,我希望能够自动填写字段trial_id,具体取决于我在数据表中添加新行的位置(对于/ trial / 1,trial_id = 1)但是我不知道如何在POST和PUT中检索此属性(或试用对象ID)。

我的postAction:

/**
 * @Rest\Post("/api_i/", name="api_i_post")
 */
public function postAction(Request $request)
{
    $data = new Intervention;
    $id = $request->get('id');
    $action = $request->get('action');
    $daadala = $request->get('daadala');
    $date = $request->get('date');
    $week = $request->get('week');
    $infopm = $request->get('info_pm');
    $comment = $request->get('comment');
    $location = $request->get('location');
    $trial = $request->get('trialid');

    $data->setAction($action);
    $data->setDaadala($daadala);
    $data->setDate($date);
    $data->setWeek($week);
    $data->setWho($infopm);
    $data->setInfoPm($comment);
    $data->setComment($location);
    $data->setTrial($trial);

    $em = $this->getDoctrine()->getManager();
    $em->persist($data);
    $em->flush();

    $lastid = $data->getId();

    $response=array("id" => $id, "status" => "success", "newid" => $lastid);
    return new JsonResponse($response);

    $view = View::create(array("newid" => $lastid, "id" => $id, "status" => "success"));
    return $this->handleView($view);

}

我的putAction

    /**
 * @Rest\Put("/api_i/{id}")
 */
public function putAction(Request $request)
{
    $data = new Intervention;
    $id = $request->get('id');
    $action = $request->get('action');
    $daadala = $request->get('daadala');
    $date = $request->get('date');
    $week = $request->get('week');
    $infopm = $request->get('info_pm');
    $comment = $request->get('comment');
    $location = $request->get('location');
    $sn = $this->getDoctrine()->getManager();
    $intervention = $this->getDoctrine()->getRepository('ErpBundle:Sponsor')->find($id);

    if (empty($intervention)) {
        return new View("Sponsor not found", Response::HTTP_NOT_FOUND);
    }
    $intervention->setAction($action);
    $intervention->setDaadala($daadala);
    $intervention->setDate($date);
    $intervention->setWeek($week);
    $intervention->setWho($infopm);
    $intervention->setInfoPm($comment);
    $intervention->setComment($location);
    $sn->flush();

    $response=array("id" => $id, "status" => "success");

    return new JsonResponse($response);
}

你能帮我解决这个问题吗?

非常感谢

回复后我的代码更新:

我在我的树枝模板中更新了这个:

  

保存:“rest-> {{path('api_i_post',{trialid:trial.id})}}”,

如果我查看ajax请求的分析器,我看到它在这里:

  

关键值

     

trialid“1”

但我仍然不知道如何在我的帖子请求中得到它(trial_id现在仍为null)

我尝试了以下内容:

    /**
 * @Rest\Post("/api_i/", name="api_i_post")
 * @Rest\RequestParam(name="trialid")
 *
 * @param ParamFetcher $paramFetcher
 * @param Request $request
 */
public function postAction(Request $request, ParamFetcher $paramFetcher)
{
    $data = new Intervention;
    $id = $request->get('id');
    $action = $request->get('action');
    $daadala = $request->get('daadala');
    $date = $request->get('date');
    $week = $request->get('week');
    $infopm = $request->get('info_pm');
    $comment = $request->get('comment');
    $location = $request->get('location');
    $trial = $paramFetcher->get('trialid');

    $data->setAction($action);
    $data->setDaadala($daadala);
    $data->setDate($date);
    $data->setWeek($week);
    $data->setWho($infopm);
    $data->setInfoPm($comment);
    $data->setComment($location);
    $data->setTrial($trial);

    $em = $this->getDoctrine()->getManager();
    $em->persist($data);
    $em->flush();

    $lastid = $data->getId();

    $response=array("id" => $id, "status" => "success", "newid" => $lastid);
    return new JsonResponse($response);

    $view = View::create(array("newid" => $lastid, "id" => $id, "status" => "success"));
    return $this->handleView($view);

}

2 个答案:

答案 0 :(得分:0)

要获得后期值,您需要在后期操作中执行此操作:

public function postAction(Request $request)
{
     $postData = $request->request->all();

然后你有一个像:

这样的值数组
$id = $postData['id'];

对于PUT,你需要这个:

public function putAction(int $id, Request $request)
{
    $putData = json_decode($request->getContent(), true);

然后去研究这样的价值:

$id = $putData['id'];

答案 1 :(得分:0)

我猜您使用的是FosRestBundle,如果是这样,您可以使用注释来检索您的网址参数:

/**
 * @Rest\Put("/api_i/{id}", requirements={"id" = "\d+"})
 */
public function putAction($id)
{
    // you now have access to $id
    ...
}

如果您想允许路线的附加参数而不是uri,可以使用带注释的RequestParam:

/**
 * @Rest\Put("/my-route/{id}", requirements={"id" = "\d+"})
 *
 * @Rest\RequestParam(name="param1")
 * @Rest\RequestParam(name="param2")
 *
 * @param ParamFetcher $paramFetcher
 * @param int $id
 */
public function putAction(ParamFetcher $paramFetcher, $id)
{
    $param1 = $paramFetcher->get('param1');
    ....
}

请务必查看fosRestBundle文档以查看您可以执行的所有操作(例如键入params,强制使用或不强制等等)