0

I need to create a route in a zend framework 2 project that helps me to create these url:

Code:

<?php

return array ( 
    'router' => array ( 
            'routes' => array ( 
                    'username' => array (
                            'type' => 'hostname',
                            'options' => array (
                                    'route' => ':username.test.:tld',
                                    'constraints' => array(
                                            'username' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'tld' => '[com|net]*',
                                    ),
                                    'defaults' => array (
                                            'controller' => 'Application\Controller\Index',
                                            'action' => 'show',
                                            'tld' => 'com'
                                    )
                            )
                    ),


                    'home' => array (
                            'type' => 'hostname',
                            'options' => array (
                                    'route' => 'www.test.:tld',
                                    'constraints' => array(
                                            'tld' => '[com|net]*',
                                    ),
                                    'defaults' => array (
                                            'controller' => 'Application\Controller\Index',
                                            'action' => 'index',
                                            'tld' => 'com'
                                    )
                            )
                    ),

            )
    ), 

In some case when I call the http://username.test.com url I need to redirect the user to the http://www.test.com by the url helper.

update:

For instance the zfUser has a specific routes to serve the user:

  • /user
  • /user/register
  • /user/logout

When I execute this code when the application starts from the http://username.test.com

<a href="<?php echo $this->url('zfcuser') ?>" title="Profilo"><i class="fa fa-user"></i><?php echo $this->translate('Your Profile') ?></a>

It creates a links like these:

and I would like this:

How I can do it?

Rikesh
  • 26,156
  • 14
  • 79
  • 87
Michelangelo
  • 1,398
  • 2
  • 14
  • 37

1 Answers1

1

The "problem" here is that you want to set the ZfcUser routes as the children of your home route. However, ZfcUser sets just routes as root and not as child_routes of yours.

The solution is is using "prototypes" and the chain_routes key. It is pretty undocumented, but you can read some info in this PR.

Basically, transform your top-level routes ("home" and "username") to a prototype and set the chain_routes key for the ZfcUser route.

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99