Edit: I originally started working on this article several months ago, however, as I was editing it, the Zend Framework team released a revamped reference guide that goes through much of what I discuss. Hopefully though, this still ends up being useful and informative for someone out there.

Edit #2: I don’t mean for this article to be followed verbatim, but rather, used as a guide to understanding Zend Framework’s routing process.

Now that I am using ZF for current projects at work, I have taken the opportunity to promote ZF with my colleagues. One colleague in particular, who has been using Code Igniter (CI), was somewhat perplexed by ZF’s seeming complexity, even after reading through the first few sections of the Reference Guide. In particular, he didn’t understand how ZF handled URIs as it is quite different from CI. So, I have put together the following, hopefully simple overview, explaining a bit about ZF’s routing and using it in conjunction with Zend_Config.

Out of the box, ZF routes URIs by turning the first segment into the controller and the second segment into the action, unless you have specified a module directory, in which case, the first segment becomes the module, the second segment the controller and the third segment the action. Any segments beyond that are converted into key-value pairs that are accessible through the front controller’s request object.

Therefore, something like http://www.vayanis.com/post/view/id/101/title/new-post is translated into a request for the ‘post’ controller, and ‘view’ action with two request parameters: ‘id’ with value ‘101’ and ‘title with value ‘new-post’. Accessing these parameters is simple:

1
2
$id = $this->_request->getParam('id');
$title = $this->_request->getParam('title');

Unfortunately, this default behavior is bad for SEO because it adds unnecessary and useless information to the URI, but like many things in ZF, there is a solution and so I turn to the Zend_Controller_Router_Rewrite.
As the documentation states:

Zend_Controller_Router_Rewrite is designed to allow for mod_rewrite-like functionality using pure php structures. It is very loosely based on Ruby on Rails routing and does not require any prior knowledge of webserver URL rewriting.

So, to simplify this URI, I can create the following route:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$route = new Zend_Controller_Router_Route(
'post/:id/:title',
array(
'controller' => 'blog',
'action' => 'post'
)
);
$router->addRoute('post', $route);

This will shorten the previous, lengthy URI from http://www.vayanis.com/post/view/id/101/title/new-post to http://www.vayanis.com/view/101/new-post

Finally, like most components, if you are not comfortable maintaining your configuration in pure php, you can instead use a INI config file. The equivalent of the previous example could be defined in an INI config file with the following code:

1
2
3
4
[production]
routes.post.route = "post/:id/:title"
routes.post.defaults.controller = blog
routes.post.defaults.action = post

To use the INI with a router, use the following:

1
2
3
4
5
6
7
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$router->addConfig(new Zend_Config_Ini('path/to/route/config/file', 'production'), 'routes');