Zend Framework website.com/username

后端 未结 2 1109
再見小時候
再見小時候 2021-01-28 18:17

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 we

相关标签:
2条回答
  • 2021-01-28 18:27

    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 讨论(0)
  • 2021-01-28 18:46

    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);
                }
    
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题