ZF2: Zend Framework 2 Full URL including host name

孤者浪人 提交于 2019-12-02 19:29:43

You can use the option force_canonical on the router. All router options go into the third parameter of the url helper:

url($route, $params, $options)

So you can so something like this:

$this->url('myroute', array('id' => 123), array('force_canonical' => true))

I found this article with some interesting ways:

1) without parameters use an empty array:

// 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) note the differences between:

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

and

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

3) starting from the route

// The "register" route has the following route: /register
echo $this->serverUrl($this->url('register')); 
// Output: http://mydomain.com/register

There is a Zend\View\Helper\ServerUrl to create full url in zend view. Try below code in your view template.

<?php echo $this->serverUrl()?>
Michał Maluga

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!