ThinkPHP5 响应输出
来源:互联网 发布:c 语言编程软件 编辑:程序博客网 时间:2024/06/14 04:43
响应(Response)对象用于动态响应客户端请求,控制发送给用户的信息。通常用于输出数据给客户端或者浏览器。
ThinkPHP5的 Response响应对象由 think\Response类或者子类完成,ThinkPHP的 App::run()方法的返回值是一个 Response对象实例,最终会调用 Response对象的 send方法完成输出。
自动输出
大多数情况,我们不需要关注Response对象本身,只需要在控制器的操作方法中返回数据即可,系统会根据default_return_type和default_ajax_return配置决定响应输出的类型。
默认的自动响应输出会自动判断是否AJAX请求,如果是的话会自动输出default_ajax_return配置的输出类型。
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return $data;
}
}
由于默认是输出Html输出,所以访问页面输出结果为:
修改配置文件,添加:
// 默认输出类型
'default_return_type' => 'json',
再次访问的输出结果为:
修改输出类型为xml:
// 默认输出类型
'default_return_type' => 'xml',
则输出结果变成:
现在明白为什么控制器的操作方法中不要直接echo输出,而要使用return返回了。
手动输出
在必要的时候,可以手动控制输出类型和参数(也是建议的方式),这种方式较为灵活。
例如如果需要指定json输出类型,可以使用下面的方式:
<?php
namespace app\index\controller;
use think\Response;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return Response::create($data, 'json');
}
}
或者直接使用系统提供的助手函数输出json:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data);
}
}
使用助手函数不受配置参数设置的影响,页面输出的结果为:
{"name":"thinkphp","status":"1"}
默认的情况下发送的http状态码是200,如果需要返回其它的状态码,可以使用:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data, 201);
}
}
或者发送更多的响应头信息:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data, 201, ['Cache-control' => 'no-cache,must-revalidate']);
}
}
也支持使用下面的链式调用的方式:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data)->code(201)->header(['Cache-control' => 'no-cache,must-revalidate']);
}
}
默认支持的输出类型包括:
输出类型 快捷方法 对应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
所以,同样的可以使用 xml方法输出 XML数据类型:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return xml($data, 201);
}
}
如果你只需要输出一个html格式的内容,可以直接使用
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = 'Hello,ThinkPHP!';
return response($data);
}
}
每一种输出类型其实对应了一个不同的Response子类(response()函数对应的是Response基类),也可以在应用中自定义Response子类满足特殊需求的输出。
这些助手函数的返回值都是Response类或者子类的对象实例,所以后续可以调用Response基类或者当前子类的相关方法,后面我们会讲解相关方法。
关于view助手函数的详细用法我们还会在下一讲中进行更详细的讲解。
ThinkPHP5的 Response响应对象由 think\Response类或者子类完成,ThinkPHP的 App::run()方法的返回值是一个 Response对象实例,最终会调用 Response对象的 send方法完成输出。
自动输出
大多数情况,我们不需要关注Response对象本身,只需要在控制器的操作方法中返回数据即可,系统会根据default_return_type和default_ajax_return配置决定响应输出的类型。
默认的自动响应输出会自动判断是否AJAX请求,如果是的话会自动输出default_ajax_return配置的输出类型。
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return $data;
}
}
由于默认是输出Html输出,所以访问页面输出结果为:
修改配置文件,添加:
// 默认输出类型
'default_return_type' => 'json',
再次访问的输出结果为:
修改输出类型为xml:
// 默认输出类型
'default_return_type' => 'xml',
则输出结果变成:
现在明白为什么控制器的操作方法中不要直接echo输出,而要使用return返回了。
手动输出
在必要的时候,可以手动控制输出类型和参数(也是建议的方式),这种方式较为灵活。
例如如果需要指定json输出类型,可以使用下面的方式:
<?php
namespace app\index\controller;
use think\Response;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return Response::create($data, 'json');
}
}
或者直接使用系统提供的助手函数输出json:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data);
}
}
使用助手函数不受配置参数设置的影响,页面输出的结果为:
{"name":"thinkphp","status":"1"}
默认的情况下发送的http状态码是200,如果需要返回其它的状态码,可以使用:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data, 201);
}
}
或者发送更多的响应头信息:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data, 201, ['Cache-control' => 'no-cache,must-revalidate']);
}
}
也支持使用下面的链式调用的方式:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return json($data)->code(201)->header(['Cache-control' => 'no-cache,must-revalidate']);
}
}
默认支持的输出类型包括:
输出类型 快捷方法 对应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
所以,同样的可以使用 xml方法输出 XML数据类型:
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = [
'name' => 'thinkphp',
'status' => '1'
];
return xml($data, 201);
}
}
如果你只需要输出一个html格式的内容,可以直接使用
<?php
namespace app\index\controller;
class Index
{
public function hello()
{
$data = 'Hello,ThinkPHP!';
return response($data);
}
}
每一种输出类型其实对应了一个不同的Response子类(response()函数对应的是Response基类),也可以在应用中自定义Response子类满足特殊需求的输出。
这些助手函数的返回值都是Response类或者子类的对象实例,所以后续可以调用Response基类或者当前子类的相关方法,后面我们会讲解相关方法。
关于view助手函数的详细用法我们还会在下一讲中进行更详细的讲解。
阅读全文
0 0
- ThinkPHP5 响应输出
- ThinkPHP5 响应输出
- ThinkPHP5 响应输出
- ThinkPHP5.0-响应
- ThinkPHP5.0---------请求和响应
- Thinkphp5学习(24)模型输出
- ThinkPHP5学习(6)请求和响应
- thinkphp5 之数据操作和模板输出
- thinkphp5输出执行的sql语句
- ThinkPHP5在SAE环境下的日志输出
- ThinkPHP5的debug配置和视图输出字符串内容替换
- [thinkPHP5项目实战_10]栏目列表输出和删除
- thinkphp5 使用__PUBLIC__等进行模板渲染输出
- Struts 2 用XSLT输出XML响应
- 关于Yaf响应输出的总结
- 请求参数、响应输出中文乱码问题
- java导出文件,输出响应字节数组
- ThinkPHP5 既有输入参数又有输出参数的存储过程该如何调用
- 【WEB】实战之html与css扁平化风格博客
- sublime text3安装并配置node.js环境
- 拓扑排序模板 POJ-2367 Genealogical tree
- 40道面试题
- Java多线程同步五种方法
- ThinkPHP5 响应输出
- 购物车实现流程
- Spring Boot 快速入门
- FSNS数据格式
- 最全的linux基础学习视频
- #ifndef #define #endif
- 二叉树的翻转 Python
- 每日一题(18)—— 指定地址存入数据
- 个人对背包问题的二进制优化问题的理解