Zend_Rest_Route and hierarchical routes

混江龙づ霸主 提交于 2019-12-06 08:56:16

You can manage route with a plugin.

For example, you can try something like this (not tested with ZF 1.9):

In your bootstrap, add this function (to declare the plugin)

public function _initPlugins(){
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new Application_Plugin_PRoutage());
}

with this example, the application/plugins folder, create the PRoutage.php plugin like this:

class Application_Plugin_PRoutage extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {   
        if (preg_match('/(users)(.*)(items)/', $request->getPathInfo())){

            $request->setControllerName('items'); // itemsController

            if ($request->isGet()){
                if (preg_match('/(users\/)(.*)(items\/)([0-9].)/', $request->getPathInfo())){
                    // http://example.org/users/234/items/34 //this point to showAction of itemsController
                    $request->setActionName('show');
                }
                else{
                    // http://example.org/users/234/items //this point to indexAction of itemsController
                    $request->setActionName('index');
                }
            } elseif ($request->isPost()){
                // http://example.org/users/234/items //this point to postAction of itemsController
                $request->setActionName('post');
            } elseif ($request->isPut()){
                // http://example.org/users/234/items/34 //this point to putAction of itemsController
                $request->setActionName('put');
            }elseif ($request->isDelete()){
                // http://example.org/users/234/items/34 //this point to deleteAction of itemsController
                $request->setActionName('delete');
            }
            $request->setDispatched(true) ;
        }
        elseif (preg_match('/(users)/', $request->getPathInfo())){

            $request->setControllerName('users'); // usersController

            if ($request->isGet()){
                if (preg_match('/(users\/)([0-9].)/', $request->getPathInfo())){
                    // http://example.org/users/234 //this point to getAction of usersController 
                    $request->setActionName('get');
                }
                else{
                    // http://example.org/users/ //this point to indexAction of usersController
                    $request->setActionName('index');
                }
            } elseif ($request->isPost()){
                // http://example.org/users/ //this point to postAction of usersController
                $request->setActionName('post');
            } elseif ($request->isPut()){
                // http://example.org/users/234 //this point to putAction of usersController
                $request->setActionName('put');
            }elseif ($request->isDelete()){
                // http://example.org/users/234 //this point to deleteAction of usersController
                $request->setActionName('delete');
            }
            $request->setDispatched(true) ;
        }
    }      

}

Of course, you can improve it.

I hope it will help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!