我正在使用Laravel 5.1,我无法使用它的请求注入。
如果我print_r($request->all())
,我得到一个空数组:
Array
(
['] =>
)
但是当我检查Request::getContent()
时,它显示我有内容。
{"test": "test"}
为什么会这样?我以前从来没有遇到过这个问题。
我的控制器方法
public function state(Requests'CheckState $request) {
print_r($request->all());
print_r($request->getContent());
}
我的请求class CheckState extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
];
}
}
发送原始JSON数据到Laravel时,一定要指定Content-Type: application/json
这是因为Request
类检查JSON内容的方式:
/**
* Determine if the request is sending JSON.
*
* @return bool
*/
public function isJson()
{
return Str::contains($this->header('CONTENT_TYPE'), '/json');
}
如果省略报头,框架假定请求是明文。