thinkphp6-响应
响应输出
路由直接返回
添加路由响应 route/app.php
Route::get('hello/:name', function ($name) {
return 'Hello,' . $name . '!';
});
测试与结果
http://127.0.0.1:8000/hello/huyongjian
Hello,huyongjian!
控制器返回
控制器
<?php
namespace app\controller;
class Index
{
public function index($name='thinkphp')
{
return 'hello '. $name;
}
}
测试结果
http://127.0.0.1:8000/index?name=xiaoming
hello xiaoming
方法
输出类型 快捷方法 对应Response类
HTML输出 response \think\Response
渲染模板输出 view \think\response\View
JSON输出 json \think\response\Json
JSONP输出 jsonp \think\response\Jsonp
XML输出 xml \think\response\Xml
页面重定向 redirect \think\response\Redirect
附件下载 download \think\response\File
响应参数
设置数据
json()->data($data);
设置状态码
json($data)->code(201);
设置头部信息
json($data)->code(201)->header([
'Cache-control' => 'no-cache,must-revalidate'
]);
设置Cookie
response()->cookie('name', 'value', 600);
重定向
return redirect('http://www.thinkphp.cn');
return redirect((string) url('hello',['name' => 'think']));
<?php
namespace app\controller;
class Index
{
public function index()
{
return redirect('/hello')->with('name','thinkphp');
}
public function hello()
{
$name = session('name');
return 'hello,'.$name.'!';
}
}
文件下载
public function download()
{
// download是系统封装的一个助手函数
return download('test.txt', 'text.txt');
}
// 设置300秒有效期
return download('image.jpg', 'my')->expire(300);
方法 描述
name 命名下载文件
expire 下载有效期
isContent 是否为内容下载
mimeType 设置文件的mimeType类型
force 是否强制下载(V6.0.3+)

浙公网安备 33010602011771号