views:

52

answers:

3

I'm developing a site which has urls like this:

site.com/james
site.com/james/photos

Schema for this urls are this: site.com/USERNAME/APPLICATION

Both username and application names are checking from db yb program logic. I set the route for this but Zend is routing all requests to this route.

But there is same controllers and admin module. When i go to site.com/admin, it looks for username "admin". Or when i go to site.com/james/profile, it tries to find the application named "profile". But there is a action in UserController for this.

How can i implement this feature? For example a front controller which looks for names of Controllers and dispatch the requst to them if controller exists?

+2  A: 

Generally, if you do this, you'll have to reinstate the 'default' routes afterwards for them to work. This literally means adding a route that matches /admin and goes to the admin module.

This soon becomes a pain to maintain if your app is quite big. You can make things easier by changing your url schemes to something like site.com/user/james and site.com/user/james/photos.

One alternative which I use for content driven websites is to route all requests that don't match an existing controller to the default controller/action:

$front->setParam('useDefaultControllerAlways', true);

By default, unrouteable requests will map to IndexController::indexAction

David Caunt
I think this is just a workaround. There must be some way to do this.
cnkt
This is how routing works - if you use a top level route (from /) then you have to reinstate all the other routes. Otherwise you'll need a custom router and additional code to check if modules, controllers and actions exist. Further, what will you do if a username matches a module or controller of your application?
David Caunt
"Further, what will you do if a username matches a module or controller of your application?" That's the point. What i want is: "First look for modules and controllers. If there is no module or controller found. Then assume it's a username. But i cant do it.
cnkt
+2  A: 

Have a look at overriding the Zend_Controller_Action __call($method, $args) method which is invoked if the desired action cannot be found. You could put your site.com/USERNAME/APPLICATION checking logic in the __call method of, say, the default controller (or wherever makes most sense) and go back to using the default route.

jah
A: 

I'd use route chaining. Remove default routes, and add it at the end of the chain.

Eventually a front controller plugin to handle the conditions.

takeshin