Leo Galleguillos
Leo Galleguillos

Reputation: 2730

ZF2 URL helper for subdomain

I have a website with four subdomains:

http://www.domain.com/
http://dev.www.domain.com/     # (dev environment for www.domain.com)
http://blog.domain.com/
http://dev.blog.domain.com/    # (dev environment for blog.domain.com)

I would like to add links from my www subdomain to my blog subdomain such that:

Is there an easy way to do this using Zend Framework 2's URL helper?

I have tried setting up my route for the blog subdomain as follows:

'blog' => [
    'type'    => 'Zend\Mvc\Router\Http\Hostname',
    'options' => '[:1st.]blog.domain.com',
    'constraints' => [
        '1st' => 'dev',
    ]
],

and the URL helper call from the view on the dev.www subdomain looks like this:

$this->url('blog', [], null, true);

I thought that passing true as the fourth argument would keep existing parameters intact. However, the URL being generated is always http://blog.domain.com/ rather than the desired http://dev.blog.domain.com/

Any ideas? I wonder if I found a bug in ZF2. Thank you.

Upvotes: 1

Views: 120

Answers (1)

Leo Galleguillos
Leo Galleguillos

Reputation: 2730

I figured it out. The reason why the parameter did not remain intact is because it did not exist in the www subdomain. So, I needed to update my www subdomain routes as follows:

'www' => [
    'type' => 'Zend\Mvc\Router\Http\Hostname',
    'options' => [
        'route' => '[:dev.]www.worksessions.com',
        'constraints' => [
            'dev' => 'dev',
        ),
    ),
    'may_terminate' => false,
    'child_routes' => [
        // Routes which were literal and part of www go here.
    ],

And now when using the URL view helper to navigate from www to blog or dev.www to dev.blog, the dev parameter remains intact! Voilà!

Upvotes: 1

Related Questions