zend framework routing in models

by Gabi Solomon

this post is more of a personal best practice and thought to share it with you guys. I like to do things this way, and think this is the best aproach, but i might be wrong and i would like your feedback on this :D .

So in Zend Framework first of all you are recommended to always use the routers assemble method to generate the Urls. Wich is very good since you will then have an easy way of updating an URL, for example when the SEO guy tells you to switch 2 url params around.

The problem is where you place your call to the router ?

One way to go about it is to place it in the controller, or in the view. But i think that is not the best ideea since this would mean in case you would want to change the route, the name for example, you would need to change it in all the places you made it.

A second choice would be to have the router call in a function somewhere that you would call from everywhere you would want the URL generated. But that seems to me like an extra layer.

My approch.

Almost all the time that URL is related to a model. For example if u have an article model. then you could make a method buildUrl :
public function buildUrl()
$router = Zend_Controller_Front::getInstance()->getRouter();

$url = $router->assemble(
‘article_id’ => $this->id,
return $url;

and then in your controllers and views you would just do:

which looks nicer and you are keeping the call to the router in one place.

Thats about it, i know this might look like pretty simple stuff for most of you, but i hope some will find this useful.

  • http://codeutopia.net/ Jani Hartikainen

    Yeah I think this is a good approach. I have used a similar way in my code as well – mostly inspired by the Django framework’s get_absolute_url model function.

  • http://www.eatmybusiness.com ronny stalker

    Thanks for doing these Zend Framework posts. Nice to see how others are doing it.

    I have not got as far as using the router to build urls in my app, but in my opinion, its a bit worrying to see the Model having so much knowledge of the Controller.

    I’m still in theory-land here. But if i had this problem – i would attempt to keep the call to get the front controller instance in an action controller and, from there, ask the model to give me some values to play with.

    Just my thoughts after having read the Domain-Driven Design book (by Eric Evans) re isolating the domain.


    Having said all that, you’ve been putting these things in practice whereas I’m still researching the theoretical , where everything seems much simpler. ;o)

  • http://www.gsdesign.ro/ Gabi Solomon

    The ideea behind having the url generation in the model is the DRY principale ( http://en.wikipedia.org/wiki/Don%27t_repeat_yourself ).
    So if you ever have to change the way you write your URLS, there is only one place in witch you have to make modifications.

  • http://www.modularhomesnetwork.com/ Modular Homes

    I like this site for more time i visit in this site!

  • http://www.findprefab.com/prefab-homes Prefab House

    Yeah, It’s a great frameworks.

  • http://www.findprefab.com/ Prefab Homes

    Damn! This could be my best crackpot idea of all time!!

  • http://www.modularhomesnetwork.com Manufactured Home

    Awesome, Thanks! I was looking for this sort of thing.