企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
#### 响应输出 ##### 路由直接返回 添加路由响应 route/app.php ~~~php Route::get('hello/:name', function ($name) { return 'Hello,' . $name . '!'; }); ~~~ 测试与结果 ~~~bash http://127.0.0.1:8000/hello/huyongjian ~~~ ~~~undefined Hello,huyongjian! ~~~ ##### 控制器返回 控制器 ~~~php <?php namespace app\controller; class Index { public function index($name='thinkphp') { return 'hello '. $name; } } ~~~ 测试结果 ~~~perl http://127.0.0.1:8000/index?name=xiaoming ~~~ ~~~undefined hello xiaoming ~~~ ##### 方法 ~~~mipsasm 输出类型 快捷方法 对应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 ~~~ #### 响应参数 设置数据 ~~~scss json()->data($data); ~~~ 设置状态码 ~~~scss json($data)->code(201); ~~~ 设置头部信息 ~~~haskell json($data)->code(201)->header([ 'Cache-control' => 'no-cache,must-revalidate' ]); ~~~ 设置Cookie ~~~scss response()->cookie('name', 'value', 600); ~~~ #### 重定向 ~~~kotlin return redirect('http://www.thinkphp.cn'); ~~~ ~~~lisp return redirect((string) url('hello',['name' => 'think'])); ~~~ ~~~php <?php namespace app\controller; class Index { public function index() { return redirect('/hello')->with('name','thinkphp'); } public function hello() { $name = session('name'); return 'hello,'.$name.'!'; } } ~~~ #### 文件下载 ~~~csharp public function download() { // download是系统封装的一个助手函数 return download('test.txt', 'text.txt'); } ~~~ ~~~kotlin // 设置300秒有效期 return download('image.jpg', 'my')->expire(300); ~~~ ~~~armasm 方法 描述 name 命名下载文件 expire 下载有效期 isContent 是否为内容下载 mimeType 设置文件的mimeType类型 force 是否强制下载(V6.0.3+) ~~~