0
votes

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);

1
Are you getting some sort of error? I would say that you are on the right track using a route. Is the route not working? Please clarify the question.D-Rock
The router is working fine and the current url is executing fine. But, you misunderstood my requirement. I want to display the preferred url but not the current url. Also if someone types the same url in browser, the browser will display the user profile.Debashis

1 Answers

1
votes

Routes are checked in reverse order, so you can change user route to be just :username but then the router has no way of knowing that example.com/registration is 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:

$router->addRoute('registration',
    new Zend_Controller_Router_Route('registration/:action', array(
        'module' => 'default',
        'controller' => 'registration',
        'action' => 'index'
    ))
);

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