What is the exact difference between Yii::app()->getRequest()->pathInfo
and Yii::app()->getRequest()->baseUrl
in Yii? Examples would help.

- 6,293
- 2
- 40
- 68

- 260
- 5
- 13
-
possible duplicate of [What exactly is PATH\_INFO in PHP?](http://stackoverflow.com/questions/2261951/what-exactly-is-path-info-in-php) – Marc B Oct 11 '13 at 19:03
-
3No this is Yii-specific. – Aditya Oct 11 '13 at 19:06
-
Right, this is not a duplicate. Please do **not** close. – Michael Härtl Oct 12 '13 at 08:46
2 Answers
pathInfo Yii Class Referenсe | CHttpRequest#pathInfo
Returns the path info of the currently requested URL. This refers to the part that is after the entry script and before the question mark. The starting and ending slashes are stripped off.
E.g. your URL looks like
http://example.com/index.php/abc/def/?qwe=123
Then your "pathInfo" will look like
abc/def
baseUrl Yiic Class Referenсe | CHttpRequest#baseUrl
Returns the relative URL for the application. This is similar to scriptUrl except that it does not have the script file name, and the ending slashes are stripped off.
To understand it please refer to CHttpRequest docs and to $_SERVER docs.
public function getBaseUrl($absolute=false)
{
if($this->_baseUrl===null)
$this->_baseUrl=rtrim(dirname($this->getScriptUrl()),'\\/');
return $absolute ? $this->getHostInfo() . $this->_baseUrl : $this->_baseUrl;
}
and
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;
}
and
'SCRIPT_NAME' Contains the current script's path. This is useful for pages which need to point to themselves. The FILE constant contains the full path and filename of the current (i.e. included) file.
'SCRIPT_FILENAME' The absolute pathname of the currently executing script.
E.g. your URL looks like
http://example.com/index.php/abc/def/?qwe=123
Then your "baseUrl" will look like empty string ("") because
1. $_SERVER['SCRIPT_NAME'] is "/index.php"
2. Yii::app()->request->getScriptUrl() is "/index.php"
3. Yii::app()->request->getBaseUrl() is ""
E.g. your URL looks like (imagine that you put your application not in a root web folder for current host but in a subfolder "customfolder")
http://example.com/customfolder/index.php/abc/def/?qwe=123
Then your "baseUrl" will look like "/customfolder" because
1. $_SERVER['SCRIPT_NAME'] is "/customfolder/index.php"
2. Yii::app()->request->getScriptUrl() is "/customfolder/index.php"
3. Yii::app()->request->getBaseUrl() is "/customfolder"

- 2,347
- 4
- 21
- 23
Let me quote my comment from the reference manual for some examples:
In both following cases the application is installed in a subdirectory somefolder
.
showScriptName=false
Browser URL: 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=true
Browser URL: 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

- 8,428
- 5
- 35
- 62