Made of Everything You're Not

No, not the flute playing Eric Lamb; the guitar playing, PHP programmer Eric Lamb. The better Eric Lamb.
  • Home
  • Projects
  • Portfolio
  • Resume
« 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

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.

Bookmark and Share

Related Posts

Stand Alone ExpressionEngine Authentication
Importing Legacy Users Into ExpressionEngine
Connect to Multiple Databases with Zend Framework
CartThrob 2.0 Beta Fun
ExpressionEngine and the Mystery of M00o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr

Tags: php, view helper, zend framework

This entry was written by Eric Lamb and posted on Friday, April 9th, 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, or trackback from your own site.

10 Comments

  1. Artem Nezvigin says:
    April 9, 2010 at 7: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
  2. Michael Stelly says:
    April 12, 2010 at 7: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
  3. Gugu says:
    July 1, 2010 at 2:36 pm

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

    Reply
  4. Nonilion says:
    September 21, 2010 at 6:22 am

    Thx. i was looking for that evrywhere.

    Reply
  5. teasy says:
    September 23, 2010 at 8:19 am

    cheers, was looking for it :)

    Reply
  6. Foz says:
    October 20, 2010 at 8: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
  7. Thomas Rudolf says:
    October 25, 2010 at 9:06 am

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

    Reply
  8. jennique adams says:
    July 1, 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
  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
  10. Sprayer says:
    May 7, 2012 at 5:59 am

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

    Reply

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

Copyright © 2008 - 2012 Eric Lamb - All rights reserved