dongtan6695 2017-06-19 12:08
浏览 32

php / symfony:检索POST / PUT api的url中的属性

For a datatable I use in a page (webix datatable), I have to use a REST API.

My url is for example: http://localhost:8000/trial/1

In this page to make the api call I use the following:

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

With the GET method, I retrieve for a trial (/trial/1), many interventions which are loaded from a database and filled in the datatable.

With this datatable, I'm able to "add a new row". It uses the POST method (save: "rest->{{ path('api_i_post') }}")

When I add a new row, I'd like to be able to get the field trial_id filled in automatically, depending from where I add a new row in the datatable (for /trial/1, trial_id = 1) but I don't know how to retrieve this attribute (or the trial object id), in a POST and a PUT.

My 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);

}

And my 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);
}

Can you help me with this issue?

Thank you very much

Update of my code after the replys:

I have update this in my twig template:

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

If I look in the profiler of the ajax request, I see it is here:

Key Value

trialid "1"

But I still don't figure how to get it in my post request (the trial_id is still null right now)

I've tried the following:

    /**
 * @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条回答 默认 最新

  • douxiu6835 2017-06-19 12:10
    关注

    To get post value you need to do this inside your post action:

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

    Then you have an array of value like:

    $id = $postData['id'];
    

    For the PUT you need this:

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

    And then to treieve a value like this:

    $id = $putData['id'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)