Symfony 2设置请求内容并检索它

时间:2015-07-06 11:12:35

标签: php json symfony

我在Symfony2工作,我想用JSON字符串查询Request的内容,并使用即:$request->get('name')来访问内容。

JSON字符串:

$string = '{
            "name":"Bob",
            "surname":"White",
            "email":"bobwhite@gmail.com",
            "nationality":"",
        }';

$request = new Request ($query = array(), $request = array(), $attributes = array(), $cookies = array(), $files = array(), $server = array(), $content = $string);

var_dump($request->get('name'));die;

对我来说,上面的方法是有效的,但var转储给了我null ...有谁能看到我在哪里错了...?

1 个答案:

答案 0 :(得分:4)

你想要这样的东西吗?

use Symfony\Component\HttpFoundation\Request;

$input = '{
            "name":"Bob",
            "surname":"White",
            "email":"bobwhite@gmail.com",
            "nationality":""
        }';
$data = json_decode($input, true);


$request = new Request (array(), $data);

var_dump($request->request->get('name'));

die;