Made of Everything You're Not

Personal blog of PHP programmer Eric Lamb.
  • Blog
  • Portfolio
« Remove Boonex Footer From Dolphin CMS
Expression Engine Escaping Madness »

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.

Related Posts

ACM Interactions
Stand Alone ExpressionEngine Authentication
MSRC
Importing Legacy Users Into ExpressionEngine
Nesting Platform

Tags: php view helper zend framework

This entry was written by Eric Lamb and posted on April 09th, 2010 at 2:34 pm and is filed under Code, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response below.

16 Comments

  1. Artem Nezvigin says:
    April 09, 2010 at 07:56 pm

    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!

    Reply | Quote
  2. Michael Stelly says:
    April 12, 2010 at 07:26 am

    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. ;-(

    Reply | Quote
  3. Gugu says:
    July 01, 2010 at 02:36 pm

    Thanks for the post. Just like you I was ‘flummoxed’. Details details

    Reply | Quote
  4. Nonilion says:
    September 21, 2010 at 06:22 am

    Thx. i was looking for that evrywhere.

    Reply | Quote
  5. teasy says:
    September 23, 2010 at 08:19 am

    cheers, was looking for it smile

    Reply | Quote
  6. Foz says:
    October 20, 2010 at 08:28 am

    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

    Reply | Quote
  7. Thomas Rudolf says:
    October 25, 2010 at 09:06 am

    Your tutorial was very helpful for me. Thank you from Germany

    Reply | Quote
  8. jennique adams says:
    July 01, 2011 at 11:25 pm

    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!

    Reply | Quote
  9. danielsz says:
    September 10, 2011 at 12:32 pm

    Where can I read in the documentation that the array should have these parameters? Because I can’t find anywhere.

    Reply | Quote
  10. Sprayer says:
    May 07, 2012 at 05:59 am

    http://www.sprayer-us.com/  link?

    Reply | Quote
  11. Paul says:
    June 09, 2012 at 05:56 am

    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.

    Reply | Quote
  12. Rohan says:
    June 20, 2012 at 10:47 pm

    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.

    Reply | Quote
  13. ParĂ¢metros do URL View Helper no Zend Framework &# says:
    June 27, 2012 at 12:55 pm

    [...] Blog do Eric Lamb [...]

    Reply | Quote
  14. Foobar says:
    October 11, 2012 at 05:55 pm

    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?

    Reply | Quote
  15. awesome url shortener says:
    March 22, 2013 at 04:47 am

    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!

    Reply | Quote
  16. bagssqqqs1oo says:
    April 09, 2013 at 09:09 pm

    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

    Reply | Quote

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • February 2012
    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
  • Advertisement

Copyright © 2008 - 2013 Eric Lamb - All rights reserved