2017-06-19 33 views
0

對於我在頁面中使用的數據表(Webix數據表),我必須使用REST API。php/symfony:在POST/PUT API中檢索URL中的屬性

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

在這個頁面中,使API調用我用的是以下幾點:

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

用GET方法,我檢索試用(/試驗/ 1 ),許多幹預從數據庫加載並填充到數據表中。

有了這個數據表,我可以「添加一個新行」。它使用POST方法(保存:「rest - > {{path('api_i_post')}}」)

當我添加一個新行時,我希望能夠自動填充字段trial_id ,取決於我在數據表中添加新行的位置(for/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); 
} 

你能幫我這個問題?

謝謝我的代碼非常

更新的回信後:

我有更新這在我的樹枝模板:

節省了:「休息 - > {{路徑('api_i_post',{trialid:trial。ID})}}」,

如果我期待在Ajax請求的探查,我看到它是在這裏:

核心價值

trialid 「1」

但我仍然不知道如何得到它在我的發佈請求(trial_id現在仍然是空的)

我試過了fol降脂:

/** 
* @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); 

} 

回答

0

爲了獲得崗位價值,你需要做到這一點您的帖子行動中:

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

然後你的價值就像一個數組:

$id = $postData['id']; 

對於把你需要這個:

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

然後到treieve這樣的值:

$id = $putData['id']; 
+0

感謝您的回覆!我認爲我以這種方式檢索的ID是數據庫干預的ID。我正在嘗試獲取數據庫試用版的ID(數據庫干預中的關係trial_id)。試用標識是url中的標識(或本頁的試用對象),這是我在發佈請求時難以添加的標識。任何線索? – jonibegoode

+0

裏面放了例如我已經在聲明參數裏面加了$ id自動獲取的@jonibegoode –

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文檔,看一切你可以做(​​這樣的s輸入參數,使它們是強制性的,等等......)

+0

謝謝你的回覆!我不確定這是我需要的。路由中的id是表干預的ID。我試圖檢索trial_id(這是頁面試用版的URL的ID)/ 1我不知道關於ParamFetcher,感謝指向我,但我不太清楚如何使用它。我已經在原始消息中更新了我的代碼,但它仍然沒有得到trial_id – jonibegoode