php 将 JSON 发布到 Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21219482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting JSON To Laravel
提问by Joel_Blum
I am trying to make a post request of json to Laravel. The request is received on the server however when I try to access a property I get: "Trying to get property of non-object". On the client I'm using angularjs.
我正在尝试向 Laravel 发出 json 的 post 请求。请求是在服务器上收到的,但是当我尝试访问一个属性时,我得到: "Trying to get property of non-object"。在客户端上,我使用 angularjs。
angular:
角度:
$http.post($rootScope.globals.basePath+"login/handleAjax",{"id" : obj.values[0].id,"profileUrl" : obj.values[0].publicProfileUrl}).success(function(data){
console.log("got success!",data);
});
laravel:
拉拉维尔:
class LoginController extends BaseController {
/*User logs in to linkedin and sends his id through ajax to this function*/
public function handle_ajax() {
$data = Input::all();
*//Clockwork is just a debugging extension I'm using*
Clockwork::info($data->id); **//"Trying to get property of non-object".**
}
Note: I can see in Fiddler that the JSON being sent is valid and that it reaches the controller+method (http 200).
注意:我可以在 Fiddler 中看到发送的 JSON 是有效的,并且它到达了控制器+方法 (http 200)。
The post request itself (As seen with Fiddler)
发布请求本身(如 Fiddler 所示)
Headers:
Accept: application/json, text/plain, */*
...
Text View:
{"id":"my id","profileUrl":"http://www.linkedin.com/pub/yoel-blum/51/373/76"}
采纳答案by Jacob Budin
Laravel's Input::allmethod returns an associative array, not an object of PHP's stdClass.
Laravel 的Input::all方法返回一个关联数组,而不是 PHP 的 stdClass 对象。
$data = Input::all();
$data['id']; // The ID of the request
回答by jdunk
Update: Laravel 5
更新:Laravel 5
Please note as of Laravel 5.0, the Inputfacade has been removed from the official documentation(and in 5.2 it was also removed from the listof default Facades provided) in favor of directly using the Requestclass that Inputinvokes, which is Illuminate\Http\Request.
请注意,从Laravel 5.0 开始,Inputfacade 已从官方文档中删除(在 5.2 中,它也从提供的默认 Facades列表中删除),以支持直接使用调用的Request类Input,即Illuminate\Http\Request.
Also, as of the Laravel 5.1documentation, all references to the Requestfacadehave been removed, again in preference of using the Illuminate\Http\Requestinstance directly, which it encourages you to dovia dependency injection in either:
此外,从Laravel 5.1文档开始,所有对RequestFacade 的引用都已被删除,再次倾向于Illuminate\Http\Request直接使用实例,它鼓励您通过以下任一方式的依赖注入来执行:
...your Controller Method:
...你的控制器方法:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function update(Request $request, $id)
{
$data = $request->json()->all();
}
}
...or a Route Closure (as of 5.3):
...或路线关闭(从5.3 开始):
use Illuminate\Http\Request;
Route::get('/', function (Request $request) {
$data = $request->json()->all();
});
json() and ParameterBag
json() 和 ParameterBag
It's worth noting that $request->json()returns an instance of Symfony\Component\HttpFoundation\ParameterBag, and that ParameterBag's ->all()method returns an associative array, and not an object as the OP expected.
值得一提的是,$request->json()返回的一个实例Symfony\Component\HttpFoundation\ParameterBag,这ParameterBag的->all()方法返回一个关联数组,而不是一个对象的要求的OP。
So one would now fetch the rough equivalent of $_POST['id']as follows:
所以现在可以获取$_POST['id']如下粗略的等价物:
$data = $request->json()->all();
$id = $data['id'];
`Input` and `Request` facades: Current Status
`Input` 和 `Request` 门面:当前状态
Both facades have been removed from the official documentation (as of 5.1), and yet they both also remainin the source codewith no 'deprecated' label.
这两个外观都已从官方文档中删除(从5.1 开始),但它们都保留在源代码中,没有“弃用”标签。
As mentioned earlier, Inputwas removedas a default facade ('alias') in 5.2, but as of 5.4, the Requestfacade remains a default.
如前所述,在 5.2 中作为默认外观(“别名”)Input被删除,但从 5.4 开始,Request外观仍然是默认的。
This seems to imply that one couldstill use the Requestfacade to invoke methods on the Request instance(e.g. Request::json()), but that using dependency injection is simply now the officially preferred method.
这似乎意味着人们仍然可以使用RequestFacade 来调用 Request实例上的方法(例如Request::json()),但是使用依赖注入现在只是官方首选的方法。
回答by Pete Thorne
To expand (and correct) the above, in Laravel 5 you would retrieve JSON as shown:
为了扩展(和更正)上述内容,在 Laravel 5 中,您将检索 JSON,如下所示:
public function handle_ajax(Request $request) {
$data = (object) $request->json()->all();
Clockwork::info($data->id);
}
In non-trivial examples you might want to also validate your input first.
在重要的示例中,您可能还想首先验证您的输入。

