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

    • Toggler
      • on February 4, 2010
    • Book review - jQuery 1.3 with PHP
      • on January 6, 2010
    • 2009 in a few words
      • on January 2, 2010
    • Bad Firebug!
      • on December 21, 2009
    • Posterous
      • on December 2, 2009
    • Chaining routes in Zend Framework
      • on November 27, 2009
    • Zend Framework bug hunt days
      • on November 22, 2009
    • Zend Framework 1.8 Web Application Development book review
      • on November 17, 2009
    • A book review
      • on October 11, 2009
    • Playing with Zend_Navigation and routes
      • on August 9, 2009
  • Recent Comments

    • Aryashree Pritikrishna
      • on January 28th @ 9:10 am
    • Michl
      • on January 15th @ 10:09 am
    • Robert
      • on January 2nd @ 1:36 pm
    • Ivan
      • on January 2nd @ 1:33 pm
    • Keith Pope
      • on January 1st @ 11:57 am
    • Jani Hartikainen
      • on December 29th @ 8:55 am
    • johnjbarton
      • on December 22nd @ 1:01 am
    • Robert
      • on December 21st @ 11:55 pm
    • René Silva
      • on December 21st @ 11:47 pm
    • Robert van Drunen
      • on December 21st @ 6:37 pm
  • Tags

    • php
    • framework
    • zend
    • example
    • random
    • about
    • site
    • ubuntu
    • blog
    • introduction
    • book
    • wordpress
    • linux
    • apache
    • lamp
    • setup
    • review
    • open source
    • svn
    • comic
  • Categories

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

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • August 2009
    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework

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!
Other posts you might be interested in: Starting with Zend Framework - part 2, A Zend_Captcha example, Login example with Zend_Auth, Playing with Zend_Navigation and routes, Chaining routes in Zend Framework, or just wonder on through the archives...
If you liked this post, you can buy me a cup of coffee!
Tags: example, framework, helper, link, php, url, view, zend.
Categories: Development, Programming.
Subscribe to the feed.

Comments: 7

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 [...]

  • Andrew

  • February 10th, 2009

Try to use something like this: $this->url(’param2′ => ‘value2′, ‘default’, false), and be happy

  • umpirsky

  • July 31st, 2009

Small improvement:

class Umpirsky_View_Helper_Qurl extends Zend_View_Helper_Abstract {
/**
* Generates an url given the name of a route, honors query string.
*
* @param array $urlOptions Options passed to the assemble method of the Route object.
* @param mixed $name The name of a Route to use. If null it will use the current Route
* @param bool $reset Whether or not to reset the route defaults with those provided
* @return string Url for the link href attribute.
*/
public function qurl(array $urlOptions = array(), $name = null, $reset = false, $encode = true) {
$url = $this->view->url($urlOptions, $name, $reset, $encode);
$requestUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
$query = parse_url($requestUri, PHP_URL_QUERY);
if($query != ”) {
$url .= ‘/’;
$pairs = explode(’&’, $query);
foreach ($pairs as $pair) {
$url.= str_replace(’=', ‘/’, $pair) . ‘/’;
}
}

return $url;
}
}

You use it same way as Zend_View_Helper_Url, just one letter diference :

qurl(array(’param2′ => ‘value2′)); ?>

  • Jobinma

  • August 6th, 2009

What about the solution I proposed on ZF Forums :

http://www.zfforums.com/zend-framework-components-13/core-infrastructure-19/how-can-i-pass-variable-while-using-zend_paginator-2589.html#post11796

Sounds a lot easier!

Leave a Reply

 

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