和重定向一样,你可以使用 response() 帮助函数 or Response facade .
response()->make()
return response()->make('Hello world', 200, $headers);
response()->json() and ->jsonp()
return response()->json('Hello world', 200, $headers, $options=0);
response()->download(), ->streamDownload(), and ->file()
// 下载 file501751.pdf 并 重命名为 myFile.pdf
return response()->download('file501751.pdf','myFile.pdf');
// 浏览器直接打开文件 (pdf 图片等 浏览器支持的格式)
return response()->file('file501751.pdf');
// 如果你想下载外部服务中的内容,但不想把内容写到自己的服务器硬盘 ,可以使用 response()->streamDownload()
return response()->streamDownload(function () {
echo DocumentService::file('myFile')->getContent();
}, 'myFile.pdf');
