WTF
(Driverzip)
November 3, 2010, 11:32am
1
Hello,
I have a class Controller extendes CController.
Fore example, make an easy function:
public function init()
{
$sRefUrl = CHttpRequest::getUrlReferrer(); // Is OK
$sUrl = CHttpRequest::getUrl(); // exeption "Property \"SiteController._url\" is not defined."
$sReqUrl = CHttpRequest::getRequestUri(); // exeption "Property \"SiteController._requestUri\" is not defined."
}
Where I’m wrong and why I can use CHttpRequest::getUrlReferrer() but can’t use others?
Thanks a lot in advance.
WTF
(Driverzip)
November 3, 2010, 12:06pm
2
public function init()
{
$sRefUrl = CHttpRequest::getUrlReferrer();
$sUrl = yii::app()->getRequest()->getUrl();
$sReqUrl = yii::app()->getRequest()->getRequestUri();
}
Above code worx fine. But still not clear, why static methods not worx. May be some guru can explain this behavior?
diggy
(Digger A)
November 3, 2010, 12:33pm
3
Because if you look at source of framework/web/CHttpRequest.php
Public function doesn’t call any internal private variable.
public function getUrlReferrer()
{
return isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:null;
}
That works fine, so, when you call this function without creating the class instance it just return you http referer string.
But when you look at getUrl function:
public function getUrl()
{
if($this->_url!==null)
return $this->_url;
else
{
if(isset($_SERVER['REQUEST_URI']))
$this->_url=$_SERVER['REQUEST_URI'];
else
{
$this->_url=$this->getScriptUrl();
if(($pathInfo=$this->getPathInfo())!=='')
$this->_url.='/'.$pathInfo;
if(($queryString=$this->getQueryString())!=='')
$this->_url.='?'.$queryString;
}
return $this->_url;
}
}
You’ll see that there’ used private variable “private $_url;” which can’t be accessed without creating an instance of class.
When you try to use this code, then all should be fine:
$request = CWebApplication::getRequest();
var_dump($request->getUrlReferrer().$request->getUrl().$request->getRequestUri());