Yii中baseUrl和pathInfo之间的区别

时间:2013-10-11 18:59:29

标签: php yii

Yii中Yii::app()->getRequest()->pathInfoYii::app()->getRequest()->baseUrl之间的确切区别是什么?例子会有所帮助。

2 个答案:

答案 0 :(得分:3)

pathInfo Yii Class Referenсe | CHttpRequest#pathInfo

  

返回当前请求的URL的路径信息。这是指在输入脚本之后和问号之前的部分。剥离起始和结束斜杠。

E.g。您的网址看起来像

http://example.com/index.php/abc/def/?qwe=123

然后你的“pathInfo”看起来像

abc/def

baseUrl Yiic Class Referenсe | CHttpRequest#baseUrl

  

返回应用程序的相对URL。这与scriptUrl类似,只是它没有脚本文件名,并且末尾斜杠被剥离。

要了解它,请参阅CHttpRequest文档和$_SERVER文档。

public function getBaseUrl($absolute=false)
{
    if($this->_baseUrl===null)
        $this->_baseUrl=rtrim(dirname($this->getScriptUrl()),'\\/');
    return $absolute ? $this->getHostInfo() . $this->_baseUrl : $this->_baseUrl;
}

public function getScriptUrl()
{
    if($this->_scriptUrl===null)
    {
        $scriptName=basename($_SERVER['SCRIPT_FILENAME']);
        if(basename($_SERVER['SCRIPT_NAME'])===$scriptName)
            $this->_scriptUrl=$_SERVER['SCRIPT_NAME'];
        elseif(basename($_SERVER['PHP_SELF'])===$scriptName)
            $this->_scriptUrl=$_SERVER['PHP_SELF'];
        elseif(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName)
            $this->_scriptUrl=$_SERVER['ORIG_SCRIPT_NAME'];
        elseif(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false)
            $this->_scriptUrl=substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
        elseif(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0)
            $this->_scriptUrl=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
        else
            throw new CException(Yii::t('yii','CHttpRequest is unable to determine the entry script URL.'));
    }
    return $this->_scriptUrl;
}

  

'SCRIPT_NAME'包含当前脚本的路径。这很有用   需要指向自己的页面。 FILE 常量   包含当前的完整路径和文件名(即包含)   文件。

     

'SCRIPT_FILENAME'当前正在执行的绝对路径名   脚本。

E.g。您的网址看起来像

http://example.com/index.php/abc/def/?qwe=123

然后你的“baseUrl”看起来像空字符串(“”),因为

1. $_SERVER['SCRIPT_NAME'] is "/index.php"
2. Yii::app()->request->getScriptUrl() is "/index.php"
3. Yii::app()->request->getBaseUrl() is ""

E.g。您的URL看起来像(想象您将应用程序放在当前主机的根网站文件夹中,但放在子文件夹“customfolder”中)

http://example.com/customfolder/index.php/abc/def/?qwe=123

然后你的“baseUrl”看起来像“/ customfolder”,因为

1. $_SERVER['SCRIPT_NAME'] is "/customfolder/index.php"
2. Yii::app()->request->getScriptUrl() is "/customfolder/index.php"
3. Yii::app()->request->getBaseUrl() is "/customfolder"

答案 1 :(得分:0)

让我引用参考手册中的my comment作为一些例子:

在以下两种情况下,应用程序都安装在子目录somefolder中。

showScriptName =假

浏览器网址:http://www.example.com/somefolder/contact?search=term

baseUrl:        /somefolder
hostInfo:       http://www.example.com
pathInfo:       contact
queryString:    search=term
requestUri:     /somefolder/contact?search=term
scriptFile:     /www/www.example.com/htdocs/somefolder/index.php
scriptUrl:      /somefolder/index.php
url:            /somefolder/contact?search=term

showScriptName =真

浏览器网址:http://www.example.com/somefolder/index.php/contact?search=term

baseUrl:        /somefolder
hostInfo:       http://www.example.com
pathInfo:       contact
queryString:    search=term
requestUri:     /somefolder/index.php/contact?search=term
scriptFile:     /www/www.example.com/htdocs/somefolder/index.php
scriptUrl:      /somefolder/index.php
url:            /somefolder/index.php/contact?search=term