Sunday, February 1, 2015

Return Json và Download file với HTTP Responses


Việc xử lý code download file hay trả về json sẽ rất nhanh gọn

Creating A JSON Response
(Tự động set Content-Type header = "application/json"

return response()->json(['name' => 'Steve', 'state' => 'CA']);
Creating A JSONP Response
return response()->json(['name' => 'Steve', 'state' => 'CA'])
                 ->setCallback($request->input('callback'));
Creating A File Download Response
return response()->download($pathToFile);

return response()->download($pathToFile, $name, $headers);

No comments:

Post a Comment