Posting JSON objects to Symfony 2

后端 未结 3 887
广开言路
广开言路 2020-11-27 05:35

I\'m working on a project using Symfony 2, I\'ve built a bundle to handle all my database services which passes JSON data back and forward.

My Problem/Question:

3条回答
  •  [愿得一人]
    2020-11-27 06:10

    If you want to retrieve data in your controller that's been sent as standard JSON in the request body, you can do something similar to the following:

    public function yourAction()
    {
        $params = array();
        $content = $this->get("request")->getContent();
        if (!empty($content))
        {
            $params = json_decode($content, true); // 2nd param to get as array
        }
    }
    

    Now $params will be an array full of your JSON data. Remove the true parameter value in the json_decode() call to get a stdClass object.

提交回复
热议问题