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.

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?
Thanks for the brilliant article, it really helped clarify how the URL view helper works especially as there doesn’t appear to be any proper documentation on the Zend site.
One thing I noticed is that when you use routes and a URL which is from a route with a variable in it you need to add ‘default’ in the route name rather than null otherwise it will try and add any variables from the pages URL to the urls from the view helper which causes an error. The reset value is supposed to fix this but doesn’t in this situation but by specifying the route name as default it will make it use the default route thus ensuring it doesn’t get confused with the route the URL you are on is from.
Hi guys,
What my friend Paul has mentioned here is very important and i think everyone must make a note of it, that is, by giving ‘null’ as route name and reset param as true wont help all the time..but by giving ‘default’ as route name it will definitely get the default route set in your boot-strap file and wont cause any error.
[...] Blog do Eric Lamb [...]
Is it just me or does the Zend framework suck dick? Try Yii… CHttpRequest + easy to read API docs = instant pleasure. Why are these simple methods so poorly written and downright quirky?
Somebody essentially help to make critically articles I’d state. That is the very first time I frequented your website page and to this point? I amazed with the analysis you made to create this actual post incredible. Wonderful activity!
hermes outlet Just think of it as a similar of the stylish backpacks for a much more affordable. you can discover incidents that lots of some specific packs originated from the beautician. perhaps you might prefer to buy trendy a lot of women personal belongings around the internet, consider date before. Don’t be blown away at by your date. Every firm has a lively cracking open week similarly every single year.
hermes bags the pets during college class computer program was formerly set of the Pet consider trust in 2009 in helping coaches in using and / or maybe nourishing pets. on 2010, 2,066 grants seemed to be awarded with regard to professors. the eventual objective of the software program is to arrive at 30,000 classes so energy one million children.
hermes handbags of course, allow me to catch sight of: never,no the human race supplies ahead of obtained an unrealistic car/bike/boat. not maintains nearly every guy ahead of obtained a very pricey cam to pick the one they have certainly have been. i don’t assume that many guy maintains ever sold overdosed on steeply-priced music centres, upgrading opportunities as soon as last getting an almost exact object.
Louis Vuitton Belts Online Shopping
Authentic Hermes Belt Buckle
Mens Fashion Belts
Armez Handbags
Hermes Belt Online