2012-02-13 63 views
2

我使用zend框架來開發我的項目。我想在某些模型中使用協議獲取基礎URL。我嘗試以下幾行來做到這一點。但它返回空。在模型中獲取基礎url

$front = Zend_Controller_Front::getInstance(); 
$baseUrl = $front->getBaseUrl(); 
echo $baseUrl; 

我可以從那種方式獲得基地網址。什麼是最好的解決方案來做到這一點。我也可以使用下面的行。

$baseUrl = 'http://'. $_SERVER['HTTP_HOST']; 
echo $baseUrl; 

但我想要最好的解決方案。請幫幫我。

+0

可能重複的[Zend框架$這個 - >的baseUrl( )總是返回當前頁:(爲什麼)(http://stackoverflow.com/questions/1332787/zend-framework-this-baseurl-always-returns-the-current-page-why) – PeeHaa 2012-02-13 12:11:24

+0

看看這篇文章:http://stackoverflow.com/a/1332872/1100237 – coppettim 2012-02-13 12:06:45

回答

1

要獲得方案和域您可以使用此:

$request = Zend_Controller_Front::getInstance()->getRequest(); 
$url = $request->getScheme() . '://' . $request->getHttpHost(); 

URL的其餘部分就可以產生這樣的:

$serverUrlHelper = new Zend_View_Helper_ServerUrl(); 
$yourUrl = $serverUrlHelper->serverUrl(
$this->getHelper('url')->url(
     array(
      'module' => 'yourmodule', 
      'controller' => 'yourcontroller', 
      'action' => 'youraction', 
     ), 
     null, // the route 
     true));