Search code examples
phpzend-frameworkzend-framework2zend-route

ZF2- Dynamic base route


I'm trying to create a dynamic route in a ZF2 project. It will be something like "domain.com/companyurl/products". The company url is dynamic. I did it:

'company' => array(
     'type' => 'Segment',
     'options' => array(
         'route' => '[/:company]',
         'defaults' => array(
             'controller' => 'IndexController',
             'action'     => 'index',
          ),
      ),
      'may_terminate' => true,
      'child_routes' => array(
          ...
      ),

 ),

But I always have to pass the company parameter in a route.

$this->url('company/products', array('company' => 'companyurl'));

Is there some way to specify a base route at the runtime, like a base url, then all route will follow it? Something like this:

$this->url('products');

or

$this->url('company/products');

In the both cases I already specified the base route value.

I hope you understand what I mean. Thanks.


Solution

  • There is a $reuseRouteParams option that you can use in the URL helper:

    $this->url($name, $params, $options,$reuseMatchedParameters);
    

    If you set this to true it will reuse the previous route match value of companyUrl.

    You can read more on this in the docs here.