Made of Everything You're Not

Personal blog of PHP programmer Eric Lamb.
  • Blog
  • Portfolio

Archive for April, 2010

Zend Framework URL View Helper

Posted in Code, Programming on April 09th, 2010 by Eric Lamb – 16 Comments

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.

  • 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