开发者

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 p开发者_运维问答lease help me out?

Many thanks in advance


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

        }

    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜