1、Request请求对象
(1)获取Request
获取方式一:助手函数
$request = request();
获取方式二:获取实例(单例模式))
use think\Request;
$request = Request::instance();
获取方式三:注入到方法(推荐)
use think\Request;
public function requestInfo(Request $request)
{
$request;
}
(2)Request方法
请求路径
GET http://127.0.0.1:8009/index/index/requestinfo/type/5.html?id=001
函数说明结果domain()域名http://127.0.0.1:8009pathinfo()路径带后缀index/index/requestinfo/type/5.htmlpath()路径不带后缀index/index/requestinfo/type/5method()请求类型GETisGet()是否为GET请求trueisPost()是否为POST请求falseisAjax()是否为ajax请求falseget()查询参数Array([“id”] => “001”)get(“id”)查询参数“001”param()所有参数包括postArray([“id”] => “001” [“type”] => “5”)param(‘type’)单个参数“5”post()POST参数Array()session(‘name’, ‘jack’)设置session-session()获取sessionArray([“name”] => “jack”)cookie(‘key’, ‘value’)设置cookie-cookie()获取cookieArray([“PHPSESSID”] => “734672fc1386d54105273362df904750” [“key”] => “value”)module()模块indexcontroller()控制器Indexaction()操作requestinfourl()url带查询字符串/index/index/requestinfo/type/5.html?id=001baseUrl()不带查询字符串/index/index/requestinfo/type/5.html
(3)助手函数input
input('get.id')
// 相当于
$request->get('id')
// $request->参数类型(key名,key值,函数名);
$request->get('key','value','intval');
2、响应对象Response
方式一:通过配置文件修改响应格式(整个模块生效 ) conf/api/config.php
return [
'default_return_type'=>'json'
];
方式二:动态修改返回类型
关注
打赏