YiiにおけるYii::app()->getRequest()->pathInfo
との正確な違いは何ですか? Yii::app()->getRequest()->baseUrl
例が役立ちます。
2 に答える
pathInfo Yii クラス リファレンス | CHttpRequest#pathInfo
現在リクエストされている URL のパス情報を返します。これは、エントリ スクリプトの後、クエスチョン マークの前の部分を指します。開始と終了のスラッシュは取り除かれます。
たとえば、あなたのURLは次のようになります
http://example.com/index.php/abc/def/?qwe=123
次に、「pathInfo」は次のようになります
abc/def
baseUrl Yiic クラスのリファレンス | 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' 現在実行中のスクリプトの絶対パス名。
たとえば、あなたのURLは次のようになります
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"
いくつかの例について、リファレンスマニュアルから私のコメントを引用しましょう。
次のどちらの場合も、アプリケーションはサブディレクトリにインストールされますsomefolder
。
showScriptName=false
ブラウザの 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
ブラウザの 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