Laravel Response
Laravel 支持直接返回数组或者模型(或者集合),以下两种方式最终会被自动转为 json 响应:
// 直接返回数组
Route::get('/request-json-array', function(){
$array = array('foo', 'bar');
//this route should returns json response
return $array;
});
// 或者返回模型/集合
Route::get('/request-json-model', function(){
//if 'User' is an eloquent model, this route should returns json response
return User::all();
});
//响应json
/*$data = [
'errCode'=>0,
'errMsg' =>'success',
'data' => 'yxh',
];
return response()->json($data);*/
return Response::make($contents);
return Response::make($contents, 200);
return Response::json(array('key' => 'value'));
return Response::json(array('key' => 'value'))
->setCallback(Input::get('callback'));
return Response::download($filepath);
return Response::download($filepath, $filename, $headers);
// 创建一个回应且修改其头部信息的值
$response = Response::make($contents, 200);
$response->header('Content-Type', 'application/json');
return $response;
// 为回应附加上 cookie
return Response::make($content)
->withCookie(Cookie::make('key', 'value'));