• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Ubuntu Administrator
      • on January 6, 2009
    • Login example with Zend_Auth
      • on January 5, 2009
    • 2008: Moments worth a note
      • on December 27, 2008
    • Styling the default Zend_Form layout
      • on December 23, 2008
    • Data filtering with PHP’s Filter extension
      • on December 15, 2008
    • MyUrl view helper for Zend Framework
      • on December 2, 2008
    • One day for Human Rights
      • on November 26, 2008
    • LAMP and SVN on Ubuntu 8.10
      • on November 24, 2008
    • TickTweet WordPress plug-in
      • on November 21, 2008
    • School’s out!
      • on November 13, 2008
  • Recent Comments

    • vasim
      • on January 2nd @ 3:45 pm
    • Jonathan Kushner
      • on January 2nd @ 4:48 am
    • BradGman
      • on December 31st @ 4:16 am
    • gsx
      • on December 30th @ 4:07 am
    • Robert Kubica
      • on December 28th @ 6:45 pm
    • abtris
      • on December 27th @ 2:30 pm
    • Christian
      • on December 27th @ 12:35 am
    • Fernando Bittencourt
      • on December 26th @ 3:07 pm
    • Josh
      • on December 26th @ 7:13 am
    • gimly
      • on December 21st @ 5:29 pm
  • Friends and Blogs

    • Andrew Taylor
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Matthew Weier O’Phinney
    • Nebojša Radović
    • Nemanja Avramović
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec
    • WeAreJustCreative
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Zend Framework
  • Tags

    • php
    • example
    • framework
    • zend
    • site
    • about
    • wordpress
    • ubuntu
    • random
    • introduction
    • apache
    • setup
    • linux
    • blog
    • lamp
    • mysql
    • comic
    • facebook
    • registration
    • virtualbox
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Archives

    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008

MyUrl view helper for Zend Framework

by Robert Basic on December 2nd, 2008

I started writing some boring introduction but I’ll just skip to the point.

The problem

Zend Framework’s built in URL view helper — Zend_View_Helper_Url — is discarding the query string of the URL, thus breaking some links.

Example: If I’m on a page like:
http://project/foo/bar/?param1=value1
and in the bar.phtml I use the Url helper like this:

<?= $this->url(array('param2' => 'value2')); ?>

I expect this:
http://project/foo/bar/param2/value2/?param1=value1
or something similar to this. This would be just perfect:
http://project/foo/bar/param1/value1/param2/value2
But no, it gives:
http://project/foo/bar/param2/value2/

The solution

After working on several workarounds, currently this is the best one I can think of — take the link that is created by the built-in Url helper and add the query string on that link:

<?php

// Usage:
// <?= $this->myUrl($this->url(array('param2' => 'value2'))); ?>
// Output:
// http://project/controller/action/param2/value2/?param1=value1
class Zend_View_Helper_MyUrl
{
    public function myUrl(&$url, &$toAdd = array())
    {
        $requestUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
        $query = parse_url($requestUri, PHP_URL_QUERY);
        if($query == '')
        {
            return $url;
        }
        else if(empty($toAdd))
        {
            return $url . '/?' . $query;
        }
        else
        {
            $toAdd = (array)$toAdd;
            $query = explode("&", $query);

            $add = '/?';

            foreach($toAdd as $addPart)
            {
                foreach($query as $queryPart)
                {
                    if(strpos($queryPart, $addPart) !== False)
                    {
                        $add .= '&' . $queryPart;
                    }
                }
            }
            return $url . $add;
        }
    }
}

The second parameter, $toAdd, should be an array of parameters that we want to add to the URL. Say, if I have a query string like:
?param1=value1&someotherparam=anditsvalue
but want only to add the param1=value1 to the URL, I would pass “param1” as the second parameter. Not passing anything as the second parameter will result in adding the complete query string to the URL.

This is an ugly hack to make ugly links work, but it works. Thoughts?

Cheers!

Share this post:
  • Digg
  • description
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Reddit
  • TwitThis
  • Google
  • E-mail this story to a friend!
Tags: example, framework, helper, link, php, url, view, zend.
Categories: Development, Programming.
Subscribe to the feed.

Comments: 4

Grab the comments feed

  • buck lay

  • December 2nd, 2008

A whole class to add a query string seems a bit absurd. CakePHP does all of this for you and does it right. Is there are reason why you have to use Zend Framework? I think even Symfony can do these kinds of things without any trouble. Best of luck.

  • Jasper

  • December 2nd, 2008

You say that it’s absurd to have a class that is a visual representation of a query string, but enough abstraction and need for that level of control over the query string does warrant it.

Swapping frameworks just because treatment of URLs is easier isn’t really decent enough justification, either, unless that’s the sole function of your project.

Glad you managed to get it sorted, Robert - maybe now you can shut up about it on twitter :p

  • Robert

  • December 2nd, 2008

@buck lay: ZF is my choice. Live with it. It has it’s upsides and downsides and a few WTF?!’s along the way, but that’s how it goes with everything in life.

@Jasper: I’ll shut up for a while, until something new comes up :P

Cheers!

  • Robert Basic’s Blog: MyUrl view helper for Zend Framework : WebNetiques

  • December 3rd, 2008

[...] Basic has posted a view helper for the Zend Framework he’s developed - one to more correctly handle URLs without dropping [...]

Leave a Reply

 

Robert Basic © 2009
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on January 3rd, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS