yivi
yivi

Reputation: 47300

zf2 - url view helper: specify parameters for route

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

Upvotes: 0

Views: 110

Answers (2)

yivi
yivi

Reputation: 47300

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.

Upvotes: 0

marcosh
marcosh

Reputation: 9008

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

Upvotes: 2

Related Questions