三十六、逐行阅读Yii2.0.43_Yii框架文件yii\web\Request.php(3)

一、getMethod方法,返回当前请求的方法

    /**
     * 返回当前请求的方法
     */
    public function getMethod()
    {
        if (
            isset($_POST[$this->methodParam])
            // Never allow to downgrade request from WRITE methods (POST, PATCH, DELETE, etc)
            // to read methods (GET, HEAD, OPTIONS) for security reasons.
            && !in_array(strtoupper($_POST[$this->methodParam]), ['GET', 'HEAD', 'OPTIONS'], true)
        ) {
            //通过post方法提交的隐藏字段确定请求方法
            return strtoupper($_POST[$this->methodParam]);
        }

        //请求头中获取请求方法
        if ($this->headers->has('X-Http-Method-Override')) {
            return strtoupper($this->headers->get('X-Http-Method-Override'));
        }

        // REQUEST_METHOD字段确定请求方法
        if (isset($_SERVER['REQUEST_METHOD'])) {
            return strtoupper($_SERVER['REQUEST_METHOD']);
        }

        //默认GET
        return 'GET';
    }

二、一些方法

  • getIsGet 是否GET请求
  • getIsOptions 是否OPTIONS请求
  • getIsHead 是否HEAD请求
  • getIsPost 是否POST请求
  • getIsDelete 是否DELETE请求
  • getIsPut 是否PUT请求
  • getIsPatch 是否PATCH请求
  • getIsAjax 是否Ajax请求
  • getIsPjax 是否Pjax请求
  • getIsFlash 是否Adobe Flash or Flex请求
    // 是否GET请求
    public function getIsGet()
    {
        return $this->getMethod() === 'GET';
    }

    // 是否OPTIONS请求
    public function getIsOptions()
    {
        return $this->getMethod() === 'OPTIONS';
    }

    // 是否HEAD请求
    public function getIsHead()
    {
        return $this->getMethod() === 'HEAD';
    }

    // 是否POST请求
    public function getIsPost()
    {
        return $this->getMethod() === 'POST';
    }

    // 是否DELETE请求
    public function getIsDelete()
    {
        return $this->getMethod() === 'DELETE';
    }

    // 是否PUT请求
    public function getIsPut()
    {
        return $this->getMethod() === 'PUT';
    }

    // 是否PATCH请求
    public function getIsPatch()
    {
        return $this->getMethod() === 'PATCH';
    }

    // 是否Ajax请求
    public function getIsAjax()
    {
        return $this->headers->get('X-Requested-With') === 'XMLHttpRequest';
    }

    // 是否Pjax请求
    public function getIsPjax()
    {
        return $this->getIsAjax() && $this->headers->has('X-Pjax');
    }

    // 是否Adobe Flash or Flex请求
    public function getIsFlash()
    {
        $userAgent = $this->headers->get('User-Agent', '');
        return stripos($userAgent, 'Shockwave') !== false
            || stripos($userAgent, 'Flash') !== false;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值