Zend Framework URL View Helper
I’ve been working with Zend Framework a lot lately, which is one reason for the drought in posting, and have kept running into a recurring uncertainty using the URL view helper. The URL view helper included with the Zend Framework is a little confusing at random times for me so in the hopes of making sense out of everything long term the below is a brief outline of how the Zend Framework URL view helper works and what not.

Zend Framework URL View Helper
The Zend Framework URL view helper is used to render a URL that follows the rules setup using the Zend Route module. This is nice because you can change the routes defined for your application and not have to worry about how your URLs are structured.
The basic syntax is below:
<?php $this->url(array $urlOptions = array(), $name = null, $reset = false, $encode = true); ?>
Using the above for a template below is an example of the usage and output:
<?php //outputs /a/b/c/ echo $this->url(array('module' => 'a','controller'=>'b','action'=>'c'), null, FALSE); ?>
As you can see the URL view helper outputs a simple URL to the module “a”, controller “b” and the action “c”.
Things get a little trickier when you want to pass along some variables though. By default the above example will append any existing variables, that are outside of the MVC paradigm, onto any new URLs created. For example, if the page url is the below:
/*
/a/b/c/foo/4/bar/yes
*/And you call the below call to the URL view helper:
<?php echo $this->url(array('module' => 'a2','controller'=>'b2','action'=>'c2'), null, FALSE); ?>
You’ll get the below:
/*
/a2/b2/c2/foo/4/bar/yes
*/When I first ran into this issue I was flummoxed. It was kind of a problem (to put it mildly). To get around this you have to set the “reset” value to TRUE. Doing so will keep any existing query variables out of your URL.
To add fresh variables to the URL view helper you use the below syntax:
<?php //outputs /a2/b2/c2/bar/yes echo $this->url(array('module' => 'a2','controller'=>'b2','action'=>'c2','bar'=>'yes'), null, TRUE); ?>
That will, hopefully, keep you from making the same mistake and the subsequent head bashing that would be sure to ensue.
Email
Twitter
Using named routes will help with the complexity (and shorten your markup).
I stored the application configuration file in application/config/application.ini (I exclude this from source control and use application.ini.dist to distribute a base config file w/ default and blank values).
Next to the application.ini, we have routes.ini which is imported into the front controller. This way you can define your route and reference it by name separately. Example:
user-profile.route = “user/:id”
user-profile.defaults.module = default
user-profile.defaults.controller = user
user-profile.defaults.action = profile
user-profile.reqs.id = “\d+”
Then in your view:
$this->url(array(‘id’ => 1), ‘user-profile’);
This does two things well:
1) If you pass too many arguments, they are ignored. If you pass too few, you get an error. If you omit parameters that are not required, they are given the default value you defined in your routes.ini. Great for catching things like this early, especially for complex routes with a lot of required parameters.
2) It shortens your markup considerably and if you ever need to customize a route you can always defer to the routes.ini first. You won’t have to update it 100 times across your entire app (this becomes even more helpful when you have Views that live outside of the standard MVC stack – like email templates).
It may seem cumbersome to add stuff to the routes.ini whenever you create a new action, but you’ll see that it only takes a few seconds to do so.
Good luck!
Thanks Eric and Artem,
Both the post and the comment were extremely helpful. I, too, am a ZF neophyte as well as a PHP noob. Imagine my discomposure when I slam into URL problems like this not knowing whether the issue emanates from ZF or PHP. ;-(
Thanks for the post. Just like you I was ‘flummoxed’. Details details
Thx. i was looking for that evrywhere.
cheers, was looking for it
Will the URL helper ‘sort’ name/value parameters?
Example: Module = Default, Controller = foo, Action = bar
/foo/bar/
I then have parameters “size = small” and “color = red”.
/foo/bar/size/small/color/red
However, I always want it to show up as:
/foo/bar/color/red/size/small
Your tutorial was very helpful for me. Thank you from Germany
you’re really a good webmaster. The website loading speed is amazing. It seems that you’re doing any unique trick. Moreover, The contents are masterpiece. you’ve done a excellent job on this topic!
Where can I read in the documentation that the array should have these parameters? Because I can’t find anywhere.
http://www.sprayer-us.com/ link?