Given this route configuration for a module
[code lang=”php”]
‘router’ => array(
‘routes’ => array(
‘test’ => array(
‘type’ => ‘Literal’,
‘options’ => array(
‘route’ => ‘/test’,
‘defaults’ => array(
‘__NAMESPACE__’ => ‘Test\Controller’,
‘controller’ => ‘Index’,
‘action’ => ‘index’,
),
),
‘may_terminate’ => true,
‘child_routes’ => array(
‘default’ => array(
‘type’ => ‘Segment’,
‘options’ => array(
‘route’ => ‘/[:controller[/:action]]’,
‘constraints’ => array(
‘controller’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
‘action’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
),
‘defaults’ => array(
),
),
),
),
),
),
),
[/code]
I wanted to create an URL using the ViewHelper which directly links to an action of the Index Controller, e.g.
/test/index/my-action
To achieve this, it is important (and not so obvious, maybe) to append the name of the child route to the name of the parent route separated with a slash, e.g.
[code lang=”php”]
echo $this->url(
‘test/default’,
array(‘action’ => ‘my-action’, ‘controller’ => ‘index’)
);
[/code]
otherwise the helper won’t care about the child route and only link to /test. Tricky…
First post I found with the answer. The Zend skeleton application puts a child route under the application route by default and it was certainly unclear how to utilize it in the view helper. I’ve been using ZF2 for a few years now and had never even tried to use that model of routing until recently. Thanks for the help.
I’m glad it was useful for others. Really took me a while to figure this out.
In this post I found the answer for my problem. I am amazed it is not there anywhere to be found so easily although so many people would need it.