探索不一样的“Request”

chenvle 2019-10-22 PM 916℃ 0条

序言

在ThinkPHP项目中,最常用的类肯定是Request,无论是什么样的api,都涉及到各种各样的Request请求。下面我们来探索不一样的Request请求

什么是Request

所有接口中,都会传一个对象到服务器,里面包含许多的信息,包括请求类型、http头、变量等等,我们把这个对象赋值给一个类来处理这些信息,这个类就是Request。

检测变量是否设置

可以使用has方法来检测一个变量参数是否设置,如下:

Request::has('id','get');
Request::has('name','post');

变量检测可以支持所有支持的系统变量,包括get/post/put/request/cookie/server/session/env/file

变量获取

变量获取使用\think\Request类的如下方法及参数:

use think\Request;

变量类型方法包括:

方法描述
param获取当前请求的变量
get获取 $_GET 变量
post获取 $_POST 变量
put获取 PUT 变量
delete获取 DELETE 变量
session获取 $_SESSION 变量
cookie获取 $_COOKIE 变量
request获取 $_REQUEST 变量
server获取 $_SERVER 变量
env获取 $_ENV 变量
route获取 路由(包括PATHINFO) 变量
file获取 $_FILES 变量

获取PARAM变量

// 获取当前请求的name变量
Request::param('name');
// 获取当前请求的所有变量(经过过滤)
Request::param();
// 获取当前请求的所有变量(原始数据)
Request::param(false);
// 获取当前请求的所有变量(包含上传文件)
Request::param(true);

如果你使用了依赖注入的方式,可以更简单的方式获取请求变量。

use think\Request;

class Index 
{
    public function index(Request $request)
    {
        // 获取name请求变量
        return $request->name;
    }
}

获取请求类型

在很多情况下面,我们需要判断当前操作的请求类型是GET、POST、PUT、DELETE或者HEAD,一方面可以针对请求类型作出不同的逻辑处理,另外一方面有些情况下面需要验证安全性,过滤不安全的请求。
请求对象Request类提供了下列方法来获取或判断当前请求类型:

用途方法
获取当前请求类型method
判断是否GET请求isGet
判断是否POST请求isPost
判断是否PUT请求isPut
判断是否DELETE请求isDelete
判断是否AJAX请求isAjax
判断是否PJAX请求isPjax
判断是否为JSON请求isJson(V5.1.38+)
判断是否手机访问isMobile
判断是否HEAD请求isHead
判断是否PATCH请求isPatch
判断是否OPTIONS请求isOptions
判断是否为CLI执行isCli
判断是否为CGI模式isCgi

method方法返回的请求类型始终是大写,这些方法都不需要传入任何参数。

use think\Request;

class Index 
{
    public function index(Request $request)
    {
        // 获取当前请求类型
        return $request->method;// 输出 GET/POST/PUT/REQUEST/COOKIE/SERVER/SESSION/ENV/FILE
       // 判断是否GET请求
        return $request->isGet; // 输出 true 或 false
       ...
    }
}

HTTP 头

可以使用Request对象的header方法获取当前请求的HTTP请求头信息,例如:

$info = Request::header();
echo $info['accept'];
echo $info['accept-encoding'];
echo $info['user-agent'];

HTTP请求头信息的名称不区分大小写,并且_会自动转换为-,所以下面的写法都是等效的:

$agent = Request::header('user-agent');
$agent = Request::header('User-Agent');
$agent = Request::header('User_Agent');
$agent = Request::header('USER_AGENT');

获取参数的助手函数

说到参数的获取,不得不提的两个助手函数requestinput,无需引入任何的类,在所有需要的地方调用。

use think\Request;
class Index 
{
    public function index(Request $request)
    {
        $name = $request->param('name');
        // 等价于
        $name = input('name');
        // 等价于
        $name = request()->param('name');
       ...
    }
}

备注:能不用就不要用助手函数,说不定以后的某个版本取消了,就不习惯了

拓展:文档定义的Request 方法

 * @see \think\Request
 * @mixin \think\Request
 * @method void hook(mixed $method, mixed $callback = null) static Hook 方法注入
 * @method \think\Request create(string $uri, string $method = 'GET', array $params = [], array $cookie = [], array $files = [], array $server = [], string $content = null) static 创建一个URL请求
 * @method mixed domain(bool $port = false) static 获取当前包含协议、端口的域名
 * @method mixed url(bool $domain = false) static 获取当前完整URL
 * @method mixed baseUrl(bool $domain = false) static 获取当前URL
 * @method mixed baseFile(bool $domain = false) static 获取当前执行的文件
 * @method mixed root(bool $domain = false) static 获取URL访问根地址
 * @method string rootUrl() static 获取URL访问根目录
 * @method string pathinfo() static 获取当前请求URL的pathinfo信息(含URL后缀)
 * @method string path() static 获取当前请求URL的pathinfo信息(不含URL后缀)
 * @method string ext() static 当前URL的访问后缀
 * @method float time(bool $float = false) static 获取当前请求的时间
 * @method mixed type() static 当前请求的资源类型
 * @method void mimeType(mixed $type, string $val = '') static 设置资源类型
 * @method string method(bool $method = false) static 当前的请求类型
 * @method bool isGet() static 是否为GET请求
 * @method bool isPost() static 是否为POST请求
 * @method bool isPut() static 是否为PUT请求
 * @method bool isDelete() static 是否为DELTE请求
 * @method bool isHead() static 是否为HEAD请求
 * @method bool isPatch() static 是否为PATCH请求
 * @method bool isOptions() static 是否为OPTIONS请求
 * @method bool isCli() static 是否为cli
 * @method bool isCgi() static 是否为cgi
 * @method mixed param(string $name = '', mixed $default = null, mixed $filter = '') static 获取当前请求的参数
 * @method mixed route(string $name = '', mixed $default = null, mixed $filter = '') static 设置获取路由参数
 * @method mixed get(string $name = '', mixed $default = null, mixed $filter = '') static 设置获取GET参数
 * @method mixed post(string $name = '', mixed $default = null, mixed $filter = '') static 设置获取POST参数
 * @method mixed put(string $name = '', mixed $default = null, mixed $filter = '') static 设置获取PUT参数
 * @method mixed delete(string $name = '', mixed $default = null, mixed $filter = '') static 设置获取DELETE参数
 * @method mixed patch(string $name = '', mixed $default = null, mixed $filter = '') static 设置获取PATCH参数
 * @method mixed request(string $name = '', mixed $default = null, mixed $filter = '') static 获取request变量
 * @method mixed session(string $name = '', mixed $default = null, mixed $filter = '') static 获取session数据
 * @method mixed cookie(string $name = '', mixed $default = null, mixed $filter = '') static 获取cookie参数
 * @method mixed server(string $name = '', mixed $default = null, mixed $filter = '') static 获取server参数
 * @method mixed env(string $name = '', mixed $default = null, mixed $filter = '') static 获取环境变量
 * @method mixed file(string $name = '') static 获取上传的文件信息
 * @method mixed header(string $name = '', mixed $default = null) static 设置或者获取当前的Header
 * @method mixed input(array $data,mixed $name = '', mixed $default = null, mixed $filter = '') static 获取变量 支持过滤和默认值
 * @method mixed filter(mixed $filter = null) static 设置或获取当前的过滤规则
 * @method mixed has(string $name, string $type = 'param', bool $checkEmpty = false) static 是否存在某个请求参数
 * @method mixed only(mixed $name, string $type = 'param') static 获取指定的参数
 * @method mixed except(mixed $name, string $type = 'param') static 排除指定参数获取
 * @method bool isSsl() static 当前是否ssl
 * @method bool isAjax(bool $ajax = false) static 当前是否Ajax请求
 * @method bool isPjax(bool $pjax = false) static 当前是否Pjax请求
 * @method mixed ip(int $type = 0, bool $adv = true) static 获取客户端IP地址
 * @method bool isMobile() static 检测是否使用手机访问
 * @method string scheme() static 当前URL地址中的scheme参数
 * @method string query() static 当前请求URL地址中的query参数
 * @method string host(bool $stric = false) static 当前请求的host
 * @method string port() static 当前请求URL地址中的port参数
 * @method string protocol() static 当前请求 SERVER_PROTOCOL
 * @method string remotePort() static 当前请求 REMOTE_PORT
 * @method string contentType() static 当前请求 HTTP_CONTENT_TYPE
 * @method array routeInfo() static 获取当前请求的路由信息
 * @method array dispatch() static 获取当前请求的调度信息
 * @method string module() static 获取当前的模块名
 * @method string controller(bool $convert = false) static 获取当前的控制器名
 * @method string action(bool $convert = false) static 获取当前的操作名
 * @method string langset() static 获取当前的语言
 * @method string getContent() static 设置或者获取当前请求的content
 * @method string getInput() static 获取当前请求的php://input
 * @method string token(string $name = '__token__', mixed $type = 'md5') static 生成请求令牌
 * @method string cache(string $key, mixed $expire = null, array $except = [], string $tag = null) static 设置当前地址的请求缓存
 * @method string getCache() static 读取请求缓存设置
标签: Request, ThinkPHP, PHP, PHP框架

非特殊说明,本博所有文章均为博主原创。

评论啦~