0

One of the application I am developing using Zend Framework requires the user's profile page to be accessed via website.com/username, while other pages should be accessed by website.com/controller_name/action_name

I am not too sure how can this be achieved, however, I feel this can be done with some tweaks in the .htaccess file.

Can someone here please help me out?

Many thanks in advance

user845990
  • 11
  • 1
  • 3
  • I think the best you could do would be website.com/user/username. I think you cannot just have website.com/username. – Marcin Aug 01 '11 at 11:20
  • @Marcin: Well, OP probably _could_ do it way he suggests (all "single-level" urls route to the controller/action that performs the user lookup and renders the user profile), but (as you imply) it's a pretty suspect url scheme. Your suggested scheme is much better. – David Weinraub Aug 01 '11 at 13:06

2 Answers2

2

As suggested before, you can use a custom route that will route single level requests. However, this will also override the default route. If you're using modules, this will no longer work example.com/<module>.

I have done this before but only for static pages. I wanted this:

 example.com/about 

instead of this:

example.com/<some-id>/about 

while maintaining the default route so this still works

example.com/<module>
example.com/<controller>

The way I did this was using a plugin to test if my request could be dispatched. If the request could not be dispatched using the default route, then I would change the request to the proper module to load my page. Here is a sample plugin:

class My_Controller_Plugin_UsernameRoute extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();

        if (!$dispatcher->isDispatchable($request)) {

            $username = $request->getControllerName();

            $request->setModuleName('users');
            $request->setControllerName('dashboard');
            $request->setActionName('index');
            $request->setParam('username', $username);

            /** Prevents infinite loop if you make a mistake in the new request **/
            if ($dispatcher->isDispatchable($request)) {
                $request->setDispatched(false);
            }

        }

    }
}
brady.vitrano
  • 2,256
  • 2
  • 16
  • 26
  • HI brady.vitrano Can you please tell me how and where do I can this custom plugin? Also, I am using modular based application. Many Thanks – user845990 Aug 04 '11 at 04:55
  • Here is an SO question on registering plugins. http://stackoverflow.com/questions/938930/zend-framework-1-8-recommended-way-to-register-a-plugin – brady.vitrano Aug 04 '11 at 14:15
-1

What about using Zend_Controller_Router_Route, look here the link http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard.variable-requirements

Risto Novik
  • 8,199
  • 9
  • 50
  • 66