繁体   English   中英

为什么Laravel REST控制器$ request-> input为NULL?

[英]Why Laravel REST controller $request->input is NULL?

我正在学习http://www.tutorials.kode-blog.com/laravel-5-angularjs-tutorial教程,我已经设法为我的控制器编写类似的方法:

public function update(Request $request, $id) {
    $employee = Employee::find($id);

    $employee->name = $request->input('name');
    //...
    $employee->save();

    return "Sucess updating user #" . $employee->id;
}

在教程中认为此代码有效,但实际上var_dump($ request-> input)给出了NULL。 那么 - 我应该使用什么$ request变量来获取请求的主体? 我创建了var_dump($ request),但结构难以管理。 实际上我对这个教程很不满 - 我们真的需要列出标准更新程序中的所有字段吗?

您可以使用以下命令访问输入数据:

$input = $request->all();

https://laravel.com/docs/5.2/requests#retrieving-input

但是,在使用AngularJS $http模块时,我还必须以这种方式获取输入:

$input = file_get_contents('php://input');

获取所有输入

试试吧

$request = \Input::all();

如果要从请求对象中获取单个参数,可以使用输入方法请求类来执行此操作。

$request->input("parameter_name");

但是如果你想获取所有请求参数,那么你可以使用all方法,它将返回所有请求键值对的数组

$request->all()

你错过的是,你正在调用$request->input ,因为input是Request类的方法 ,而不是属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM