如何获取Laravel 5.0中的$request->content?
How to get $request->content in Laravel 5.0?
我是 Laravel 的新手,在获取 JSON 我正在发布到 REST api 我正在写的 api 时遇到了一些问题。
更新
为清楚起见,这:
$content = json_decode($request->content);
var_dump($content);
exit;
还有returnsnull
原创
这是我的 store
方法:
public function store(Request $request)
{
// Creates a new user based on the passed JSON
// I appreciate this wont work as it's json encoded, but this was my
// last test.
// Previously I'd tried: $content = json_decode($request->content);
// but that was also null :(
$user = new User();
$user->name = $request->content["name"];
$user->email = $request->content['email'];
$user->password = $request->content['password'];
var_dump($request); exit;
// Commit to the database
$user->save();
}
这是我要发送的内容(通过:我只是休息客户端):
{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}
下面是 var_dump 呈现为响应时的结果:
protected 'cacheControl' =>
array (size=0)
empty
protected 'content' => string '{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}' (length=85)
protected 'languages' => null
protected 'charsets' => null
protected 'encodings' => null
所以我可以在 Request
对象中看到 content
,但无论我尝试什么,它始终为空。所以我的问题是,我究竟该如何访问它?!
谢谢!
您可能想要使用 $request->getContent()
。
Laravel 通常会自动解码您的 JSON。您可以使用 input()
检索值:
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $request->input('password');
还有更短的方法,您可以通过请求方法动态访问您的属性:(这可能不适用于某些名称)
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
还有其他不错的功能可用。例如 all()
或 only()
将 return 所有输入值的关联数组:
$inputs = $request->all();
// or
$inputs = $request->only('name', 'email', 'password');
作为受保护的变量/对象,您需要使用 class 上的预定义方法访问它。
您可以使用以下内容。
$request->only('name', 'email', 'password')
{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}
无效 JSON 因此 Laravel 无法对其进行解码。
从这里删除结尾的逗号[...]ng123",
然后您将能够使用上述答案中提到的任何方法,例如(假设您以 application/json 的形式发送请求)
$request->all();
$request->only();
$request->get();
如果您不以 application/json 的形式发送请求,请使用 $request->json()
我是 Laravel 的新手,在获取 JSON 我正在发布到 REST api 我正在写的 api 时遇到了一些问题。
更新 为清楚起见,这:
$content = json_decode($request->content);
var_dump($content);
exit;
还有returnsnull
原创
这是我的 store
方法:
public function store(Request $request)
{
// Creates a new user based on the passed JSON
// I appreciate this wont work as it's json encoded, but this was my
// last test.
// Previously I'd tried: $content = json_decode($request->content);
// but that was also null :(
$user = new User();
$user->name = $request->content["name"];
$user->email = $request->content['email'];
$user->password = $request->content['password'];
var_dump($request); exit;
// Commit to the database
$user->save();
}
这是我要发送的内容(通过:我只是休息客户端):
{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}
下面是 var_dump 呈现为响应时的结果:
protected 'cacheControl' =>
array (size=0)
empty
protected 'content' => string '{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}' (length=85)
protected 'languages' => null
protected 'charsets' => null
protected 'encodings' => null
所以我可以在 Request
对象中看到 content
,但无论我尝试什么,它始终为空。所以我的问题是,我究竟该如何访问它?!
谢谢!
您可能想要使用 $request->getContent()
。
Laravel 通常会自动解码您的 JSON。您可以使用 input()
检索值:
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $request->input('password');
还有更短的方法,您可以通过请求方法动态访问您的属性:(这可能不适用于某些名称)
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
还有其他不错的功能可用。例如 all()
或 only()
将 return 所有输入值的关联数组:
$inputs = $request->all();
// or
$inputs = $request->only('name', 'email', 'password');
作为受保护的变量/对象,您需要使用 class 上的预定义方法访问它。
您可以使用以下内容。
$request->only('name', 'email', 'password')
{
"name":"Steve Jobs 2",
"email":"s@trp2.com",
"password":"something123",
}
无效 JSON 因此 Laravel 无法对其进行解码。
从这里删除结尾的逗号[...]ng123",
然后您将能够使用上述答案中提到的任何方法,例如(假设您以 application/json 的形式发送请求)
$request->all();
$request->only();
$request->get();
如果您不以 application/json 的形式发送请求,请使用 $request->json()