zendframework/zf2 — pull request #6763 by nikolaposa (2014-10-14 11:18:51+00:00)
Title: Automatic hostname route adaptation 

## Problem
In situations where Hostname route is being utilized, for example, in case of a website: *www.example.com* which also have some pages on a separate subdomain: *blog.example.com*, routes definition might look like:
```php
//...
'router' => array(
'routes' => array(
'blog' => array(
'type' => 'hostname',
'options' => array(
'route' => 'blog.example.com',
'defaults' => array(
'controller' => 'Application\Controller\BlogController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'post' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:slug]',
'constraints' => array(
'slug' => '[a-zA-Z0-9_-]+'
),
'defaults' => array(
'action' => 'view'
)
)
)
)
),
'user' => array(
'may_terminate' => false,
'type' => 'literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'Application\Controller\User',
),
),
'child_routes' => array(
'view' => array(
'type' => 'segment',
'options' => array(
'route' => '/view[/:id]',
'constraints' => array(
'id' => '\d+'
),
'defaults' => array(
'action' => 'view',
),
),
),
'edit' => array(
'type' => 'segment',
'options' => array(
'route' => '/edit[/:id]',
'constraints' => array(
'id' => '\d+'
),
'defaults' => array(
'action' => 'edit',
),
),
),
),
)
),
),
//...
```
The problem occurs when URL for the ```user/view``` route is assembled on some blog page, for example:
```php
echo $this->url('user/view', array('id' => 1));
```
That URL will point to the *blog.example.com*, even if ```force_canonical``` options is supplied.

## Solution
In this situations, Router component should respond in such way that assembled URLs always point to the appropriate hostname. This can be achieved by inflecting all the other non-hostname routes into **Chain** routes, consisting of a default hostname route and the inflecting route itself.

This improvement does just that, by automating that inflection of non-hostname routes. Thereby, developer is required to supply default hostname route definition along with other ```'router'``` options, for example:
```php
'router' => array(
'default_hostname_route' => array(
'type' => 'hostname',
'options' => array(
'route' => 'www.example.com',
),
),
'routes' => array(
//route definitions...
),
),
```
As a result, assembling of a ```user/view``` route from the above example:
```php
echo $this->url('user/view', array('id' => 1));
```
... would always result in: *http://www.example.com/user/1*, regardless of a subdomain on which it is being assembled.