user845990
user845990

Reputation: 11

Zend Framework website.com/username

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

Upvotes: 0

Views: 565

Answers (2)

brady.vitrano
brady.vitrano

Reputation: 2256

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

        }

    }
}

Upvotes: 2

Risto Novik
Risto Novik

Reputation: 8295

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

Upvotes: -1

Related Questions