【发布时间】:2013-10-19 23:01:07
【问题描述】:
Yii 中Yii::app()->getRequest()->pathInfo 和Yii::app()->getRequest()->baseUrl 的确切区别是什么?示例会有所帮助。
【问题讨论】:
-
不,这是 Yii 特有的。
-
对,这不是重复的。请不要关闭。
Yii 中Yii::app()->getRequest()->pathInfo 和Yii::app()->getRequest()->baseUrl 的确切区别是什么?示例会有所帮助。
【问题讨论】:
路径信息 Yii Class Referenсe | CHttpRequest#pathInfo
返回当前请求的 URL 的路径信息。这是指进入脚本之后和问号之前的部分。开始和结束的斜线被去掉了。
例如你的网址看起来像
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' 当前执行的绝对路径名 脚本。
例如你的网址看起来像
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 ""
例如您的 URL 看起来像(假设您将应用程序不是放在当前主机的根 Web 文件夹中,而是放在子文件夹“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"
【讨论】:
让我引用参考手册中的my comment 来获取一些示例:
在以下两种情况下,应用程序都安装在子目录 somefolder 中。
浏览器网址: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
浏览器网址: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
【讨论】: