Extending Zend URL

July 8, 2008

My friend Matt Turland (http://ishouldbecoding.com) and I have each been discovering the Zend Framework for seperate projects. Matt had a bit of a headstart on me, and when I started working, he offered me a plugin class he had written, View_Helper_Route. Zend Framework’s url helper tends to be a bit finicky, and route lets you enter url’s that make a bit more sense.

ex:  $this->url(array(“controller”=> “profile”,”action” => “index”,”id” => “5”), “default”, true);

or $this->route(“index”,”profile”,array(“id”=>”12″));

Granted, at first glance neither one looks too clean. But route() can make assumptions that url() can’t. Each parameter passed in route goes through a check, (“if param is null, $request->getParamName). So if we are jumping around in the same controller, route() turns into
$this->route(“new-action”);

This allows us to create relative links, instead of having to hard-code each link up to the module level. I ran into a bit of a fix using it today though, which is why I am writing this.

The URL method has an option parameter for which route to use, passed in after the array of controller information. Most of the time, you can pass it null and it will continue routing according to the last rule it used. If you have a site-wide rewrite, this makes sense. But for specific page rules, it can be a pain. My site doesn’t need a sitewide cleanup, we only want to not write each parameter into the url bar on pages with several get variables. no big deal, right? A page like profile/index/id/12/method/add/ becomes profile/12/add. The only problem is, for every url on that page or in the site’s navigation, $url throws an error without explicitly passing the optional parameter. The error is additionally tough to read:
Fatal error: Uncaught exception ‘Zend_Controller_Router_Exception’ with message ‘id is not specified’ in /domain/library/Zend/Controller/Router/Route.php:235


There are two neat solutions I’ve implemented in a patch to View_Help_Route. The first is simply to add another optional parameter that tells the router that if no route is specified, use default.  If we need a route, we can add one.
$this->route(“index”,”profile”,array(“id”=>”12″),”profile”);
Additionally we can use the beautiful PHP magic method hasParam() to find out if this page is supposed to have a special router, and based on that, use it.
So now, any time I link to a profile, my router will always know to use the default, site standard route, or my profile route depending on the controller.
I’ve included my route injection from my bootstrap, as well as the class itself below.

If you decide to use the route class,  I recommend placing it in a library directory named after your app.
library/
Zend/
YourApp/
View/
Helper/
Route.php
Also, make sure to replace [YourApp] in the code with the name of that directory.
Router Injection

View Helper Route

Hello World

July 7, 2008

Hello everyone,

It seemed appropriate to start off this blog with a hello world post. So… hello… world. I am a PHP programmer / Web Designer / Server Maintainer / indentured servant at the Wonderful Acadiana Educational Endowment. We are a nonprofit group supporting education throughout the U.S. by hosting a nonprofit bookstore, http://booksxyz.com. We also serve the Acadiana area (where we are located) with http://cajunfun.com and http://ultoday.com.

I was encouraged to start this blog in an effort to record my issues with various platforms on the web so I won’t forget and so others find them. I work in straight PHP 5, the Zend Framework, and ezPublish for the sites I work on, and I hope that in reading this, you resolve whatever issues I might be able to help with.

Being that this is a hello world post… I’ll start by making some short notes on my recent activities as a programmer.
ezPublish: All in all, and no offense to it’s developers, but I do not like ezP. To be fair, I had little experience with the underlying framework, ezComponents, going in. However, the documentation is poor, and the templating engine is… in a word… bad. It also creates an enormous database (this is not directly an issue, lots of software needs a big database, but in this case… it is difficult to find what you are looking for in that thing).

Zend FrameWork: I started working with ZF about two weeks ago. I love it.  I have been working in PHP about two years, and I still don’t know the deep internals of the language, but this was no limitation with ZF. The documentation is superb, and, most importantly, updated regularly. It has a fluid API and just generally makes sense.

Jedit: Last thing I want to comment on is Jedit. Again, I am in love. Up until I started work with the zend framework, I used putty+vim to do all the editing on the site. A good friend of mine recommended Jedit when he found out I was working in ZF, so i thought I’d give it a shot.
My plugins are as follows: FTP, ssh console, CSSeditor, Editor Scheme Selector(just for ease on the eyes), and PHPParser Plugin. Using the setup I have, jEdit is essentially an IDE allowing me to work remotely on the server with multiple files without all the annoyance of xMing tunneling (which I have found to be unreliable).
It has code folding, a full featured file browser (which supports remote directory listing), and a really effecient docking system. Plugins are easy to install and integrate… it just generally rocks.


Follow

Get every new post delivered to your Inbox.