views:

236

answers:

2

Well i need to make it so any user can have a foo.com/username in my site where i user a Zend framework.

The thing is that i want foo.com/login and other controllers to keep working as they do, so i already validate that no user can be named as one of the controllers im going to use. The htaccess cant be changed or the mvc configuration wont work. So im using the Router.

I know that if i use something like:

$routes = array();  
        $routes['entry']  = new Zend_Controller_Router_Route_Regex('SOME REGEX HERE', 
            array(  
                'controller' => 'profile',  
                'action'     => 'show'  
            ),  
            array(  
                'id' => "dsadsa", // maps first subpattern "(\d+)" to "id" parameter  
                1 => 'id' // maps first subpattern "(\d+)" to "id" parameter  
            )  
        );  

$router->addRoutes($routes);

i can make it so everything that matches the regex is redirected, but i dont know if theres a better way(somehow chaining 2 routers) where i dont have to actually list my controllers in a very very big OR.

Any idea?

A: 

You should be able to define your username route (your above regex route) and add it to the router first, then just add your controller routes, for example:

$router->addRoute('user', new Zend_Controller_Router_Route('controller/:var'));

Zend will try to match the request to the defined routes, in reverse order. If I request does not match one of your controller routes, it will then try to match it against your regex route.

Tim Lytle
A: 

Something like this should work:

$routes = array();  

$routes['entry'] = new Zend_Controller_Router_Route(
    ':userName', 
    array(  
        'controller' => 'profile',  
        'action'     => 'show'  
    )
);

Then add all of your other routes, for /login, /logout, etc. As Tim said, Zend matches routes in reverse order, so it will try to match against your "whitelist" of routes prior to finally sending it to your "username" route.

This will break any type of error checking Zend does for making sure the controller/action exists, since everything will be routed to /profile/show if there are no route matches.

All of this could be easily avoided, however, if you could just deal with foo.com/user/<username>.

leek