ZF-244: Zend_Controller_RewriteRouter: addRoutes() for one hit route loading

Issue Type: New Feature Created: 2006-07-13T00:52:55.000+0000 Last Updated: 2007-07-05T14:43:14.000+0000 Status: Resolved Fix version(s): - 0.2.0 (29/Oct/06)

Reporter: Nick Lo (nicklo) Assignee: Michal Minicki (martel) Tags: - Zend_Controller

Related issues: Attachments:


To allow adding routes from eg a config file in one hit how about having something along the lines of...

<pre class="highlight"> 
public function addRoutes($routes=array())
    foreach($routes as $route)
        $this->_routes[ $route['name'] ] = new Zend_Controller_Router_Route($route['map'], $route['params'], $route['reqs'] );


Posted by Michael Sheakoski (michael) on 2006-07-13T02:13:55.000+0000

I don't think this should be in the RewriteRouter itself. It is trivial to implement outside of the router just by doing a foreach loop which does a $route->addRoute()

Posted by Nick Lo (nicklo) on 2006-07-13T03:17:18.000+0000

I don't disagree with you however, there just seems to be something a little laboured about having to loop over addRoute repeatedly in say a bootstrap file when I'd suspect in most cases you'd be wanting to add a list of routes anyway.

Anyway, thanks for the comment Michael, I was mainly putting the idea up for feedback.

Posted by Michal Minicki (martel) on 2006-07-13T07:40:26.000+0000

Maybe some integration with Zend_Config? Like a totaly optional loadRoutes method which will take a Zend_Config interfaced object as a parameter? It will cartainly require some standarisation in names though.

And I honestly don't know if such coupling is acceptable by Zend.

Posted by Michael Sheakoski (michael) on 2006-07-13T14:30:47.000+0000

Nick, I thought about it and how does this sound?

First make a file called something like config.routes.php that contains something like:

<pre class="literal">
$config['router']->addRoute('books', 'book/:author/:name', array(), array());
$config['router']->addRoute('films', 'film/:director/:title', array(), array());
$config['router']->addRoute('music', 'music/:composer/:title', array(), array());

Then in your bootstrap (most likely index.php):

<pre class="literal">
$router = new Zend_Controller_RewriteRouter();
$config['router'] = &$router; // this will reference the $router object you just created
include 'config.routes.php'; // all your routes will now load into $router

This way no looping is needed and is probably the fastest way to do it. Of course you could just use $router->addRoute directly in your config file but I did it this way to sort of put the config stuff in its own "namespace".

Posted by Nick Lo (nicklo) on 2006-07-13T20:33:30.000+0000

Thanks Michael S for looking into it some more. Are you thinking about it because you see a need for some means to load in one hit or just for the challenge of the solution?

Michael M/Martel ...if I'm right you are the original/main author? If so thanks to you and any other contributors, I have been getting to grips with setting up routes and it's working out nicely, improving urls and cutting down on files as I retro fit a currently running website.

I'm picturing the following vague, untested and speculative use case:

Via an admin form a user can add new routes, eg:

Name: privacy Controller: business_article

...which along the way would be interpreted as something like...

<pre class="highlight">
$router->addRoute('privacy', 'privacy', array('article_name' => 'privacy', 'controller' => 'business_article', 'action' => 'index' ) );

This would allow them to create pseudo static articles. What it would need is a storage area and therefore a means to load from that storage area so yes in some ways a route config of sorts. From my browsing of Django they seem to have this idea in their" rel="nofollow">URL dispatcher and PythonScriptForParsingUrlConf.

Another wildly speculative idea that could spin off from a storage file containing routes is a means to autogenerate stub files for an application based on those configurations. I'm thinking of the fusebox wireframing tool idea and how in theory that route configuration would have enough information to kick start the file generation for a project.

Posted by Michal Minicki (martel) on 2006-07-14T02:17:26.000+0000

Yes, you're right Nick - I'm the original author.

I think your pseudo static articles approach may be a bit inneficient, I would advise to change the route to:

<pre class="highlight">
$router->addRoute('articles', 'articles/:article_name', array('article_name' => 'main', 'controller' => 'business_article', 'action' => 'index' ) );

This way every url which resembles '' will route you to BusinessArticle controller and indexAction of it. There you will have a param named 'article_name' filled with the article name from the URL. But that's a bit off topic ;)

I have given a read to the Django manual you have pointed me to and I think it's a very nice idea to map URLs this way. There is a problem though - you have to be a regexp guru to make a full use of it and we're aiming at every John Doe programmer out there. But I like this approach and can make a custom route or even a new router once I'm finished with this one.

The URLConf, as I gather, is used primarily to store 'local' url maps which will map routes under the common root. I don't see how it is related to storing a config. I guess you have to elaborate some more because I'm not familiar with Django... yet :)

Posted by Nick Lo (nicklo) on 2006-07-14T04:09:01.000+0000

To just hang on this off-topic for a bit, I agree that article/privacy would be a simpler approach but in that example I was taking it from an existing site that has the url as and the aim/need was to keep the short url. Wordpress, for example, works in much the same way; having ultra short url's for it's "Pages" (which are in fact "static" posts) so I would foresee others wanting to retro-fit existing sites using a "static" article approach.

Back on topic and I have to admit to not knowing Django well either, I was merely referring to what you picked up on with URLConf. It does appear to be a means to store url maps which is kind of what I was referring to. Storage of url maps in some format that could be recovered and loaded quickly into the router.

I'm not suggesting ZF does it the Django way though, at this point I'm really only asking whether anyone else sees the ability to load in routes in one batch (that would likely come from some type of configuration file) as something that would be useful. Michael S has already pointed out that a simple loop over addRoute would do the job and if that is as much as people need then I can certainly work with that.

Posted by Michal Minicki (martel) on 2006-07-14T04:26:31.000+0000

Ok, the most important thing first. I see the point of loading all routes in a batch, all in all you have to do it here or there. The problem is if we are going to include this ability, it has to be easier than looping with a single foreach because it is what you have to do right know. It does makes sense to include some loading mechanism and that's why I have proposed using Zend_Config for that task. But I'm also sure SAVING routes is way out of scope for the router. And that's what they do in Django.

And now to the static pages. I have only proposed a better solution. The static mapping of "pages" as a one static string is of course possible with the current router. It's just with a lot of pages you will be forced to define a lot of routes instead of one. It will slow down every single request whether it's related to those pages or not. But still, you can define one route of ':article_name' which will match any url with only single level "directory" depth.

Posted by Nick Lo (nicklo) on 2006-07-14T05:19:16.000+0000

I agree saving routes is unnecessary.

Perhaps it would be an idea to get some feedback from the mailing list as to other's needs. Personally I'd be satisfied with something like the addRoutes method I sketched out originally that takes an array. The problem I see with using Zend config is that the url maps are fairly complex and almost require their own format rules.

Anyway, I don't think I'm sufficiently up to speed on either RewriteRouter or Config as I'm only just implementing/playing with them so I don't want to come across as pushing an idea.

Posted by Michal Minicki (martel) on 2006-07-14T07:45:05.000+0000

addRoutes has been implemented in the latest svn revision (914).

Have you found an issue?

See the Overview section for more details.


© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.