src/Leevel/Http/Request.php
public function getBaseUrl(): string
    {
        if (null !== $this->baseUrl) {
            return $this->baseUrl;
        }

        // 兼容分析
        $fileName = basename($this->server->get('SCRIPT_FILENAME', ''));

        if (basename($this->server->get('SCRIPT_NAME', '')) === $fileName) {
            $url = $this->server->get('SCRIPT_NAME');
        } elseif (basename($this->server->get('PHP_SELF', '')) === $fileName) {
            $url = $this->server->get('PHP_SELF');
        } elseif (basename($this->server->get('ORIG_SCRIPT_NAME', '')) === $fileName) {
            $url = $this->server->get('ORIG_SCRIPT_NAME');
        } else {
            $path = $this->server->get('PHP_SELF');
            $segs = explode('/', trim($fileName, '/'));
            $segs = array_reverse($segs);
            $index = 0;
            $maxCount = count($segs);

            $url = '';
            do {
                $seg = $segs[$index];
                $url = '/'.$seg.$url;
                $index++;
            } while (($maxCount > $index) && (false !== ($pos = strpos($path, $url))) && (0 !== $pos));
        }

        // 比对请求
        $requestUri = $this->getRequestUri();

        $requestUri = (string) $requestUri;
        $url = (string) $url;

        if ('' !== $requestUri && '/' !== substr($requestUri, 0, 1)) {
            $requestUri = '/'.$requestUri;
        }

        if ($url) {
            $prefix = $this->getUrlencodedPrefix($requestUri, $url);

            if (false !== $prefix) {
                return $this->baseUrl = $prefix;
            }

            $prefix = $this->getUrlencodedPrefix($requestUri, dirname($url));

            if (false !== $prefix) {
                return $this->baseUrl = rtrim($prefix, '/').'/';
            }
        }

        $basename = basename($url);

        if (empty($basename) || !strpos(rawurldecode($requestUri), $basename)) {
            return $this->baseUrl = '';
        }

        if ((strlen($requestUri) >= strlen($url)) &&
            ((false !== ($pos = strpos($requestUri, $url))) && (0 !== $pos))) {
            $url = substr($requestUri, 0, $pos + strlen($url));
        }

        return $this->baseUrl = rtrim($url, '/').'/';
    }
public function getBaseUrl(): string
    {
        if (null !== $this->baseUrl) {
            return $this->baseUrl;
        }

        // 兼容分析
        $fileName = basename($this->server->get('SCRIPT_FILENAME', ''));

        if (basename($this->server->get('SCRIPT_NAME', '')) === $fileName) {
            $url = $this->server->get('SCRIPT_NAME');
        } elseif (basename($this->server->get('PHP_SELF', '')) === $fileName) {
            $url = $this->server->get('PHP_SELF');
        } elseif (basename($this->server->get('ORIG_SCRIPT_NAME', '')) === $fileName) {
            $url = $this->server->get('ORIG_SCRIPT_NAME');
        } else {
            $path = $this->server->get('PHP_SELF');
            $segs = explode('/', trim($fileName, '/'));
            $segs = array_reverse($segs);
            $index = 0;
            $maxCount = count($segs);

            $url = '';
            do {
                $seg = $segs[$index];
                $url = '/'.$seg.$url;
                $index++;
            } while (($maxCount > $index) && (false !== ($pos = strpos($path, $url))) && (0 !== $pos));
        }

        // 比对请求
        $requestUri = $this->getRequestUri();

        $requestUri = (string) $requestUri;
        $url = (string) $url;

        if ('' !== $requestUri && '/' !== substr($requestUri, 0, 1)) {
            $requestUri = '/'.$requestUri;
        }

        if ($url) {
            $prefix = $this->getUrlencodedPrefix($requestUri, $url);

            if (false !== $prefix) {
                return $this->baseUrl = $prefix;
            }

            $prefix = $this->getUrlencodedPrefix($requestUri, dirname($url));

            if (false !== $prefix) {
                return $this->baseUrl = rtrim($prefix, '/').'/';
            }
        }

        $basename = basename($url);

        if (empty($basename) || !strpos(rawurldecode($requestUri), $basename)) {
            return $this->baseUrl = '';
        }

        if ((strlen($requestUri) >= strlen($url)) &&
            ((false !== ($pos = strpos($requestUri, $url))) && (0 !== $pos))) {
            $url = substr($requestUri, 0, $pos + strlen($url));
        }

        return $this->baseUrl = rtrim($url, '/').'/';
    }
分析基础 url.

@return string