13

在我看来,我需要绘制完整的 URL。像这样:

http://hostename.com/default/url

当我尝试使用时,$this->url('default', array(1,2,3))我只得到/index/get/. 是否有任何 Zend 方法来获取主机名或者我必须使用它$_SERVER['HTTP_HOST']

4

4 回答 4

45

您可以使用force_canonical路由器上的选项。所有路由器选项都进入 url 助手的第三个参数:

url($route, $params, $options)

所以你可以这样:

$this->url('myroute', array('id' => 123), array('force_canonical' => true))
于 2012-10-19T12:20:31.713 回答
26

我发现这篇文章有一些有趣的方式:

1)不带参数使用空数组:

// Using a route with the name "register" and the route "/register"
echo $this->url('register', array(), array('force_canonical' => true)); 
// Output: http://mydomain.com/register

2)注意以下之间的区别:

echo $this->serverUrl(); 
// Output: http://mydomain.com

// Current URL: http://mydomain.com/register
echo $this->serverUrl(true); 
// Output: http://mydomain.com/register

3)从路线出发

// The "register" route has the following route: /register
echo $this->serverUrl($this->url('register')); 
// Output: http://mydomain.com/register
于 2013-07-03T22:26:19.460 回答
14

有一个 Zend\View\Helper\ServerUrl 可以在 zend 视图中创建完整的 url。在您的视图模板中尝试以下代码。

<?php echo $this->serverUrl()?>
于 2012-06-14T09:14:28.627 回答
0

If you want to set base URL globally, you can do it using onBootstrap method:

$e->getApplication()->getMvcEvent()->getRouter()->setBaseUrl($baseUrl);

In this case Navigation helpers would also use it.

To fetch current base URL use ServerUrl helper as described in this thread:

$serverUrl = $e->getApplication()->getServiceManager()->get('ViewHelperManager')->get('ServerUrl');
$baseUrl = $serverUrl->__invoke();
于 2014-09-24T08:56:10.283 回答