Laravel 4 JSON Formatting Output

return Response::json(array(
    'status' => 200,
    'posts' => $post->toArray()
), 200);

Using the code above, I returned the data in json format.
I saw other api that return json, returning it in a formatted form.
For example:

http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=440&count=3&maxlength=300&format=json

But mine returns it in one line. How to create json in a formatted way with laravel?


Update

I still can’t verify the code until tomorrow. Therefore, I agree with the answer.

But this is api

http://laravel.com/api/class-Illuminate.Support.Facades.Response.html

and parameters:

$data
$status
$headers

Update

In fact, I modified the backlight response class to have this constant.

+4
source share
5 answers

This is possible in the current version 4.2.

Response::json($data=[], $status=200, $headers=[], $options=JSON_PRETTY_PRINT);

https://github.com/laravel/framework/commit/417f539803ce96eeab7b420d8b03f04959a603e9

+15
source

, 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);
+6

, json (, ):

return Response::make(json_encode(array(
    'status' => 200,
    'posts' => $post->toArray()
), JSON_PRETTY_PRINT))->header('Content-Type', "application/json");
+4

( ) . xDebug ( ).

JSON . Laravel .

PHP 5.4+, JSON_PRETTY_PRINT

return json_encode(array(
    'status' => 200,
    'posts' => $post->toArray()
), JSON_PRETTY_PRINT);

, Laravel api, Response:: json().

+1

Laravel 5.2

return response()->json($data=[], $status=200, $headers=[], $options=JSON_PRETTY_PRINT)
+1

All Articles