如何获取主机地址? 例如,我的网站位于:http://example.org/index/news/ 而我只想提取http://example.org/。如何获取ZF中的http主机1.12
我问ZF功能。我知道$ _SERVER ['HTTP_HOST'],但我正在寻找本地的东西。
如何获取主机地址? 例如,我的网站位于:http://example.org/index/news/ 而我只想提取http://example.org/。如何获取ZF中的http主机1.12
我问ZF功能。我知道$ _SERVER ['HTTP_HOST'],但我正在寻找本地的东西。
从控制器:
$this->getRequest()->getServer('HTTP_HOST')
这会给你example.org
,你必须围绕它添加休息。
接受的答案是非常好的,但有几件事情你可能想要记住。
$this->getRequest();
是函数/方法调用,这意味着开销不是necessairy,因为控制器具有保护$_request
属性,因此
$this->_request
getRequest
的方法是不大于暴露$_request
更小财产(这是一种公开的方法):
public function getRequest()
{
return $this->_request;
}
应该稍微更高性能。此外,如果你看看一个为getServer
方法来源:
public function getServer($key = null, $default = null)
{
if (null === $key) {
return $_SERVER;
}
return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
}
真的是在使用该方法,而无需提供一个默认值,语法糖等没有意义的。
最快的方法将总是
$_SERVER['HTTP_HOST'];
然而,结合两全其美的,最安全的(最ZF样的方式)是:
$this->_request->getServer('HTTP_HOST', 'localhost');//default to localhost, or whatever you prefer.
完整的代码你寻找可能是:
$base = 'http';
if ($this->_request->getServer('HTTPS', 'off') !== 'off')
{
$base .= 's';
}
$base .= '://'.$this->_request->getServer('SERVER_NAME', 'localhost').'/';
哪,你的情况,应引起http://expample.org/
。
see here获取SERVER PARAMS的完整列表,你可以得到它们是什么意思,值是什么...