0

My main router goes like this (simplified):

'router' => [
    'routes' => [
        'blog' => [
            'type'         => 'regex',
            'options'      => [
                'regex'         => "/(?<language>[a-z]{2})?",
                'spec'          => "/%language%",
                'defaults'      => [
                    'controller' => 'Blog\Controller\Posts',
                    'action'     => 'index'
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
           // [...] 
                        'add_post'    => [
                            'type'    => 'literal',
                            'options' => [
                                'route'    => '/admin/post/add',
                                'defaults' => [
                                    'controller' => 'Blog\Controller\Posts',
                                    'action'     => 'add'
                                ]
                            ]
                        ], // end add post
            ] // end child routes
        ] // end blog route (main route)
    ] // end routes
] // end Router

And in the template displayed on "/en/admin/post/add" I have a call to $this->url(), that ends up printing /%language%/admin/post/add.

I have the language code available on $language on my template, and I'd like to pass it on to url() so it properly constructs the the url using the spec.

Also, I'd like, if possible, not to specify the name of the route on my call to url(), so it uses the default one for $this.

How would I go around to accomplish this?

Thanks and regards

yivi
  • 42,438
  • 18
  • 116
  • 138

2 Answers2

2

You could use a segment route instead of a regex one and then use

$this->getHelperPluginManager()->getServiceLocator()->get('request')->getUri()->getPath();

in your view to print the actual route it's been used

marcosh
  • 8,780
  • 5
  • 44
  • 74
  • Any reason why I should switch to a `segment` route? As far I can see it works perfectly with the route defined as it is... – yivi Dec 24 '15 at 20:03
  • 1
    @yivi you're rigth; there's no need to shitch to a `segment` route, but from your configuration I think that it is simpler and more suitable for what you are doing. That was just a suggestion – marcosh Dec 24 '15 at 22:07
0

While @marcosh answer works, since then I've found a simpler solution:

$this->url($this->route, ['language' => $language]);

Will output what I want. Seems clearer to me.

yivi
  • 42,438
  • 18
  • 116
  • 138