How do I create foo.com/user in Zend?

Well, I need to make it so that any user can have the name foo.com/username on my site where the Zend UI is.

The point is, I want foo.com/login and other controllers to continue working as they are, so I already check that no user can be named by one of the controllers they need to use. Can't change htaccess, otherwise mvc config won't work. So I am using a 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 that everything that matches the regex is redirected, but I don't know if there is a better way (to link 2 routers somehow) where I don't have to actually list my controllers in a very large OR.

Any idea?

0


source to share


2 answers


Something like this should work:

$routes = array();  

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

      

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



This will break any type of error checking. Zend does this to ensure that the controller / action exists, as everything will route to / profile / show if there is no route match.

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

0


source


You have to define your username route (your route is higher than normal) and add it to the router first and then just add controller routes like:

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



Zend will try to match the request to specific routes in reverse order. If the request doesn't match one of your controller routes, it will try to match it against your regex route.

0


source







All Articles