Zend Framework: Generating URLs from defined routes

Permanent Link: Zend Framework: Generating URLs from defined routes 23. Februar 2009 RSS Feed for comments on RSS-Feed für Kommentare zu: Zend Framework: Generating URLs from defined routes comments feed

The Zend Controller Router allows you to easily create your own routes. That is very useful, but it seems rather unknown, that you can also use these routes to generate URLs according to their definition.

Let's assume you have a route like this:

$controller = Zend_Controller_Front::getInstance();
$router = $controller->getRouter();

$productRoute = new Zend_Controller_Router_Route(
'module' => 'default',
'controller' => 'product',
'action' => 'show'
'pid' => '\d+'
$router->addRoute('productpage', $productRoute);

In this route we parse a URL that is supposed to show a product page. The URL will look like http://www.phpdevblog.net/4815162342/Microsoft+Windows+XP+Professional for example. The Router would explode it into pid=4815162342 and name="Microsoft Windows XP Professional"

To generate a URL with the help of the Router you simply need to call the assemble method, which you can either call from the router or the definied route, but keep in mind that you will get different results!

From the router the assemble would look like this:

$link = $router->assemble(
'pid' => '4815162342',
'name' => 'Microsoft Windows XP Professional'

The result of this method is /4815162342/Microsoft+Windows+XP+Professional

The only difference, if you call the assemble method from the defined route, is that you don't need the route name in the second parameter:

$link = $productRoute->assemble(
'pid' => '4815162342',
'name' => 'Microsoft Windows XP Professional'

Here the result is 4815162342/Microsoft Windows XP Professional


womens Gravatar

23.04.2014, 23:44 o'clock

My brother recommended I might like this web site.
He was once totally right. This subbmit actually made
my day. You can not believe just how so much time I had spent
for this information! Thanks!

メンズ ファッション 獅子座s Gravatar

メンズ ファッション 獅子座
21.09.2014, 05:44 o'clock

I really like what you guys are usually up too. This sort of clever work and coverage!
Keep up the great works guys I've you guys to my personal blogroll.

Write a comment

(will not be published)