在我看来,我需要绘制完整的 URL。像这样:
http://hostename.com/default/url
当我尝试使用时,$this->url('default', array(1,2,3))
我只得到/index/get/
. 是否有任何 Zend 方法来获取主机名或者我必须使用它$_SERVER['HTTP_HOST']
?
在我看来,我需要绘制完整的 URL。像这样:
http://hostename.com/default/url
当我尝试使用时,$this->url('default', array(1,2,3))
我只得到/index/get/
. 是否有任何 Zend 方法来获取主机名或者我必须使用它$_SERVER['HTTP_HOST']
?
您可以使用force_canonical
路由器上的选项。所有路由器选项都进入 url 助手的第三个参数:
url($route, $params, $options)
所以你可以这样:
$this->url('myroute', array('id' => 123), array('force_canonical' => true))
我发现这篇文章有一些有趣的方式:
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
有一个 Zend\View\Helper\ServerUrl 可以在 zend 视图中创建完整的 url。在您的视图模板中尝试以下代码。
<?php echo $this->serverUrl()?>
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();