Passing arrays to Zend_Controller_Router

In: PHP| Web development

23 Jun 2008

WARNING: The following contains unsanctioned HACKS to the Zend Framework. Use at your own discretion.

A big advantage to using the front controller in the Zend Framework is the ability to create nicely formatted urls. Instead of an ugly GET string we can pass parameters as slash delimited key -> value pairs. For example:
www.techfounder.com/index.php?post=passing_arrays&comment=3 can be replaced with www.techfounder.com/post/passing_arrays/comment/3
(On techfounder this is actually performed via mod_rewrite rules, but the principle is the same).

The implementation of the default router in the Zend Framework (Zend_Controller_Router_Rewrite) does not allow for passing arrays in this manner though, since previously set keys get overwritten if they are declared more than once. This is somewhere between semi-annoying to very annoying, so lets get straight to hacking it into submission.

Following the internals of the routing process we find the actual parsing of the URL address inside Zend_Controller_Router_Route_Module. Line 174 is where the assignment takes place:

  1. $params[$key] = $val; //Overwrites previously set value...

Which we modify to allow passing array variables by redeclaring same key names with different values:

  1. $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key],array($val))): $val);

This short ternary statement casts the value into an array if its key was previously declared and merges it with the next value. Using this we can pass array variables in a nicely formatted URL address. For example, passing multiple posts id's:
www.techfounder.com/show/posts/5/posts/8/posts/40

I tried to create an extension of Zend_Controller_Route_Module which performs this without modifying the core library files, however to work this class would also have to implement the Zend_Controller_Router_Route_Interface interface which means declaring 6 abtract methods... way too much for simply modifying one line of code. If anyone has a better suggestion on how to accomplish what I proposed here, I am open to suggestions.

Share this article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • DZone
  • del.icio.us
  • Technorati

Comment Form

About this blog

Eran Galperin is an Internet entrepreneur and head of development @ Lionite.

In this Blog he writes about Internet ventures and web development.

  • thomas: hi i have 3 tables like a ,b c where a contains employee detail , b contains details of item con [...]
  • Glaubensfragen « bohuco.net: [...] Blog-Eintrag “Common misconceptions in web application development” beleuchtet einige imme [...]
  • Eran Galperin: Plain SQL :) of course, I make sure to properly filter and escape user input [...]
  • Arik Fraimovich: @Eran & @George - thank you for the information. Going to rewrite some queries now :) @Eran - [...]
  • George: @Arik Fraimovich: REPLACE and ON DUPLICATE KEY UPDATE are actually quite different. REPLACE actually [...]