return Response::json(array(
'status' => 200,
'posts' => $post->toArray()
), 200);
Run Code Online (Sandbox Code Playgroud)
使用上面的代码我以json格式返回数据.
我已经看到其他api的返回json在格式化视图中返回它.
喜欢:
http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=440&count=3&maxlength=300&format=json
Run Code Online (Sandbox Code Playgroud)
但是我的回归一行.如何使用laravel以格式化方式生成json?
更新
直到明天我才能测试代码.所以我会接受汤姆的答案.
但这是api
http://laravel.com/api/class-Illuminate.Support.Facades.Response.html
Run Code Online (Sandbox Code Playgroud)
和参数是,
$data
$status
$headers
Run Code Online (Sandbox Code Playgroud)
更新
实际上我修改了照明的响应类以使其保持不变.
Tra*_*ler 17
目前的4.2版本是可能的.
Response::json($data=[], $status=200, $headers=[], $options=JSON_PRETTY_PRINT);
Run Code Online (Sandbox Code Playgroud)
https://github.com/laravel/framework/commit/417f539803ce96eeab7b420d8b03f04959a603e9
我不认为Laravel允许您格式化JSON输出.然而,你可以用它做json_encode()的JSON_PRETTY_PRINT常数(可自PHP 5.4.0).这是如何做:
$array = array(
'status' => 200,
'posts' => $post->toArray()
);
return json_encode($array, JSON_PRETTY_PRINT);
Run Code Online (Sandbox Code Playgroud)
The same answer but with the json content-type (like the example in the question):
return Response::make(json_encode(array(
'status' => 200,
'posts' => $post->toArray()
), JSON_PRETTY_PRINT))->header('Content-Type', "application/json");
Run Code Online (Sandbox Code Playgroud)