0

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

doitlikejustin
  • 6,293
  • 2
  • 40
  • 68
Aditya
  • 260
  • 5
  • 13

2 Answers2

3

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"
Pave
  • 2,347
  • 4
  • 21
  • 23
0

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
Michael Härtl
  • 8,428
  • 5
  • 35
  • 62