I have a site in Zend Framework. I want to hide one particular controller name in the URL. Explaining the requirement below.
Current URL: http://abcd.com/user/john (which is a profile page of a user)
Preferred URL: http://abcd.com/john
The preferred URL will be displayed in the browser. Also, if a visitor types a username (for eg. http://abcd.com/smith) in the url, the browser will display the user profile.
I have some other controller as well in my site (for eg. http://abcd.com/registration) and don’t want to modify or hide those controller name.
I know this can be toodifficult to make a difference to recognise which is the username and which is the controller in the url, but i really want to accomplish this. Please suggest me the needful.
Please note that i am using the below html code to display user profile link.
<a href="<?php echo $site?>user/john">John</a>
Also added the below mentioned router in bootstrap.php file.
$routeUser = new Zend_Controller_Router_Route (‘user/:username/’,array(‘controller’ => ‘User’,’action’=> ‘index’));
$router->addRoute(‘user’, $routeUser);
Routes are checked in reverse order, so you can change user route to be just
:usernamebut then the router has no way of knowing thatexample.com/registrationis not a user profile, which is why this would break your other route. The easiest way to fix this would be to add another route after the user route which handles the registration request:Alternatively if you are fairly comfortable with ZF and want to handle the user requests properly without breaking the standard routes, you could create a custom route class specifically for your user profile requests. I wrote up a blog post a while ago on how to do this, see: http://tfountain.co.uk/blog/2010/9/9/vanity-urls-zend-framework