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

一、属性

1. $_pathInfo 路径信息

2. $_url 请求的相对url

    //请求路径信息
    private $_pathInfo;

    // 请求的相对url
    private $_url;

二、方法

1. getPathInfo方法,返回路劲信息

    //当前请求的路径信息
    public function getPathInfo()
    {
        if ($this->_pathInfo === null) {
            $this->_pathInfo = $this->resolvePathInfo();
        }

        return $this->_pathInfo;
    }

2. setPathInfo方法,设置路径

    // 设置当前请求的路径信息
    public function setPathInfo($value)
    {
        $this->_pathInfo = $value === null ? 
            null : 
            //去除左边的'/'
            ltrim($value, '/');
    }

3. resolvePathInfo方法,解析请求,返回路径信息

    //解析当前请求的路径信息
    protected function resolvePathInfo()
    {
        //假设当前请求 http://www.xxx.com/index.php?r=site/index

        // 1.获取相对url
        // $pathInfo结果为 /index.php?r=site/index
        $pathInfo = $this->getUrl();

        // 2. 获取 '?' 前面的字符
        // $pathInfo结果为 /index.php
        if ((
            $pos = strpos($pathInfo, '?')
            ) !== false) {
            $pathInfo = substr($pathInfo, 0, $pos);
        }

        // 3. 解码和转换编码为utf8
        $pathInfo = urldecode($pathInfo);

        // try to encode in UTF8 if not so
        // http://w3.org/International/questions/qa-forms-utf-8.html
        if (!preg_match('%^(?:
            [\x09\x0A\x0D\x20-\x7E]              # ASCII
            | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
            | \xE0[\xA0-\xBF][\x80-\xBF]         # excluding overlongs
            | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
            | \xED[\x80-\x9F][\x80-\xBF]         # excluding surrogates
            | \xF0[\x90-\xBF][\x80-\xBF]{2}      # planes 1-3
            | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
            | \xF4[\x80-\x8F][\x80-\xBF]{2}      # plane 16
            )*$%xs', $pathInfo)
        ) {
            $pathInfo = $this->utf8Encode($pathInfo);
        }

        // 4.返回脚本入口相对url
        // $scriptUrl结果为 /index.php
        $scriptUrl = $this->getScriptUrl();

        // 5. $baseUrl结果为 ''
        $baseUrl = $this->getBaseUrl();

        if (strpos($pathInfo, $scriptUrl) === 0) {
            // $pathInfo结果为 ''
            $pathInfo = substr($pathInfo, strlen($scriptUrl));
        } elseif ($baseUrl === '' || strpos($pathInfo, $baseUrl) === 0) {
            $pathInfo = substr($pathInfo, strlen($baseUrl));
        } elseif (isset($_SERVER['PHP_SELF']) && strpos($_SERVER['PHP_SELF'], $scriptUrl) === 0) {
            $pathInfo = substr($_SERVER['PHP_SELF'], strlen($scriptUrl));
        } else {
            throw new InvalidConfigException('Unable to determine the path info of the current request.');
        }

        if (strncmp($pathInfo, '/', 1) === 0) {
            $pathInfo = substr($pathInfo, 1);
        }

        return (string) $pathInfo;
    }

4. utf8Encode方法,将ISO-8859-1字符串转换成utf8编码

    /**
     * Encodes an ISO-8859-1 string to UTF-8
     * @param string $s
     * @return string the UTF-8 translation of `s`.
     * @see https://github.com/symfony/polyfill-php72/blob/master/Php72.php#L24
     */
    private function utf8Encode($s)
    {
        $s .= $s;
        $len = \strlen($s);

        for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
            switch (true) {
                case $s[$i] < "\x80": $s[$j] = $s[$i]; break;
                case $s[$i] < "\xC0": $s[$j] = "\xC2"; $s[++$j] = $s[$i]; break;
                default: $s[$j] = "\xC3"; $s[++$j] = \chr(\ord($s[$i]) - 64); break;
            }
        }

        return substr($s, 0, $j);
    }

5.getAbsoluteUrl方法,返回请求的绝对url

    //返回绝对url
    public function getAbsoluteUrl()
    {
        return $this->getHostInfo() . $this->getUrl();
    }

6. getUrl方法,返回相对url

    /**
     * 返回当前请求的相对url
     * 示例:请求url为http://www.xxx.com/index.php?r=site/index
     * 则返回 /index.php?r=site/index
     */
    public function getUrl()
    {
        if ($this->_url === null) {
            $this->_url = $this->resolveRequestUri();
        }

        return $this->_url;
    }

7. setUrl方法,设置url

    //设置相对url
    public function setUrl($value)
    {
        $this->_url = $value;
    }

8. resolveRequestUri方法,返回uri部分

    /**
     * 返回uri部分
     */
    protected function resolveRequestUri()
    {
        if ($this->headers->has('X-Rewrite-Url')) { // IIS
            $requestUri = $this->headers->get('X-Rewrite-Url');
        } elseif (isset($_SERVER['REQUEST_URI'])) {
            $requestUri = $_SERVER['REQUEST_URI'];
            if ($requestUri !== '' && $requestUri[0] !== '/') {
                $requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri);
            }
        } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI
            $requestUri = $_SERVER['ORIG_PATH_INFO'];
            if (!empty($_SERVER['QUERY_STRING'])) {
                $requestUri .= '?' . $_SERVER['QUERY_STRING'];
            }
        } else {
            throw new InvalidConfigException('Unable to determine the request URI.');
        }

        return $requestUri;
    }

总结:

 阅读了2个属性和8个方法:

  • $_pathInfo 路径信息
  • $_url 请求的相对url
  • getPathInfo方法,返回路劲信息
  • setPathInfo方法,设置路径
  • resolvePathInfo方法,解析请求,返回路径信息
  • utf8Encode方法,将ISO-8859-1字符串转换成utf8编码
  • getAbsoluteUrl方法,返回请求的绝对url
  • getUrl方法,返回相对url
  • setUrl方法,设置url
  • resolveRequestUri方法,返回uri部分
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值