ZF2 URL View Helper and Child Routes
In Zend Framework 2, you might have routes that have child routes. For instance, a register route may be a child route of a user route. What if you wish to link to the child route by using the URL view helper? The view helper’s documentation is not of much help in this regard.
What you can do is to use the parent route’s name followed by a forward slash and then the child route’s name.
$this->url('parent_route_name/child_route_name');
In the case of the previous example, the URL of the child route can be built like this:
$this->url('user/register');
Any parameters used in the route can be passed as an array as the second parameter. Here is the method signature of the URL view helper:
url($name, $urlParams, $routeOptions, $reuseMatchedParams)
Lastly, I just want to quickly point your attention to the last parameter, because I found this to be useful in some situations. As the name strongly indicates, the $reuseMatchedParams parameter adds the parameters that matched the route to the constructed URL. For example, you might want to specify that a form’s action attribute should point to the current page, which was matched with one or more parameters. Instead of having to make these parameters available in the view script via the controller, this can all be done automatically for us by passing true as the last parameter.
Let us say that a route profile requires two parameters, giving an URL of the structure /profile/123/abc. Below is demonstrated how one can easily use the URL view helper to build an URL which includes the route’s matched parameters.
echo $this->url('profile', array(), array(), true); // Output: /profile/123/abc
Hopefully you get the point. Thank you for reading.
Here is what you will learn:
- Understand the theory of Zend Framework in details
- How to implement an enterprise-ready architecture
- Develop professional applications in Zend Framework
- Proficiently work with databases in Zend Framework
- ... and much more!
6 comments on »ZF2 URL View Helper and Child Routes«
Thanks, man!
Thanks! Hooked me right up. The “default” routes is a nice way to do drop in controllers in the old way and have em just work, but using the url helper puzzles you until you get this parent/child route syntax.
Thank you so much for this. I have spent hours trying to get my routes/navigation and the form action URL to work:
An exception has been thrown during the rendering of a template (“Route with name “request” does not have child routes”) in …
I was doing:
$this->url(‘route/action’);
The extra parameters setting $reuseMatchedParams to true fixed it.
$this->url(‘route’, array(), array(), true);
The action got appended automatically.
Glad it worked out for you. Thanks for reading! :-)
When I use *segment* child-route-name , for exemple [:controller]/[:action],
I can’t use it with :
$this->url(‘parent_route_name/controller_name/action_name’);
Is there a problem ?
Hello Gilles,
I assume that you are trying to match the default route provided by the Zend Skeleton Application? If that is not the case, then please post your route configuration. Otherwise, you will notice that the route name is application and the name of the child route is default, where the former is a literal route, and the latter is a segment route. You can match it like this:
$this->url(‘application/default’, array(‘controller’ => ‘user’, ‘action’ => ‘add’));
I tested this and it works for me.