1

For my current Application Setup i am trying to create a routing for Standard Controllers and Rest Controllers.

This is my Structure.

/application
  /modules
    /module
      /controllers
        /Admin
        /Api

To call a Controller within the admin Sub Folder:

/module/admin_controller/action

I want to change this to:

/admin/module/controller/action

In my current setup:

/* application.ini */
;Default route
resources.router.routes.defaultmodule.type = Zend_Controller_Router_Route_Module
resources.router.routes.defaultmodule.defaults.module = "default


; APi route
resources.router.routes.api.type = Zend_Controller_Router_Route
resources.router.routes.api.route = ":api" 
resources.router.routes.api.reqs.api = "api"
resources.router.routes.apichain.type = Zend_Controller_Router_Route_Chain
resources.router.routes.apichain.chain = "api, defaultmodule"

; Admin route
resources.router.routes.admin.type = Zend_Controller_Router_Route
resources.router.routes.admin.route = ":admin" 
resources.router.routes.admin.reqs.admin = "admin"
resources.router.routes.adminchain.type = Zend_Controller_Router_Route_Chain
resources.router.routes.adminchain.chain = "admin, defaultmodule"


/* Plugin */
class Pwb_Plugin_ControllerRoute
    extends Zend_Controller_Plugin_Abstract
{    
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        if ($request->getParam('admin')) {
            $admin_controller = $request->getParam('admin') . '_' . $request->getControllerName();
            $request->setControllerName($admin_controller);
        }

        if ($request->getParam('api')) {
            $admin_controller = $request->getParam('api') . '_' . $request->getControllerName();
            $request->setControllerName($admin_controller);
        }
    }
}

/* Module Bootstrap */
class Acl_Bootstrap
    extends Zend_Application_Module_Bootstrap
{
    protected function _initRestRoute()
    {
        $this->bootstrap('frontController');
        $frontController = Zend_Controller_Front::getInstance();
        $restRoute = new Zend_Rest_Route(
                $frontController,
                array(),
                array(
                    'acl' => array(
                        'api_role'
                    ))
        );
        $frontController->getRouter()->addRoute('restAcl', $restRoute);
    }

}

The issue here really is that some Controllers in the API folder are Rest but not all of them.

/api/acl/role is routing correctly to the indexAction as expected.

/api/acl/role/1 is looking for the action "1" where i would expect it to route to getAction.

How could i integrate the Zend_Route_Rest from the module Bootstrap into these rulesets.

Every help would be much appreciated.

4

1 回答 1

0

我已经对多个类进行了子类化来实现这一点,并为 admin/api 创建了静态路由

Application Bootstrap
protected function _initRoute()
    {
        $front = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();

        $dispatcher = $front->getDispatcher();
        $request = $front->getRequest();

        $defaultRoute = new Pwb_Controller_Router_Route_Module(array(), $dispatcher, $request);

        //Replace default route in our router with our own route
        $router->addRoute('default', $defaultRoute);


        $adminRoute = new Pwb_Controller_Router_Route(
                ':admin',
                array(),
                array(
                    'admin' => 'admin'
                )
        );

        $adminDefault = $adminRoute->chain($defaultRoute);

        $router->addRoute('admin', $adminRoute);
        $router->addRoute('admin', $adminDefault);

        $apiRoute = new Pwb_Controller_Router_Route(
                ':api',
                array(),
                array(
                    'api' => 'api'
                )
        );

        $apiDefault = $apiRoute->chain($defaultRoute);
        $router->addRoute('api', $apiDefault);
        $router->addRoute('api', $apiRoute);
    }


Rest Route

class Pwb_Controller_Router_Rest extends Zend_Rest_Route
{ 
    public function match($request, $partial = false)
    {
        if (!$request instanceof Zend_Controller_Request_Http) {
            $request = $this->_front->getRequest();
        }
        $this->_request = $request;
        $this->_setRequestKeys();

        $path   = $request->getPathInfo();
        $params = $request->getParams();
        $values = array();
        $path   = trim($path, self::URI_DELIMITER);

        if ($path != '') {

            $path = explode(self::URI_DELIMITER, $path);
            //Determine Environment
            if($path[0] != 'api') {
                return false;
            } else {
                array_shift($path);
            }

            // Determine Module
            $moduleName = $this->_defaults[$this->_moduleKey];
            $dispatcher = $this->_front->getDispatcher();
            if ($dispatcher && $dispatcher->isValidModule($path[0])) {
                $moduleName = $path[0];
                if ($this->_checkRestfulModule($moduleName)) {
                    $values[$this->_moduleKey] = array_shift($path);
                    $this->_moduleValid = true;
                }
            }

            // Determine Controller
            $controllerName = $this->_defaults[$this->_controllerKey];
            if (count($path) && !empty($path[0])) {
                if ($this->_checkRestfulController($moduleName, 'api_' . $path[0])) {
                    $controllerName = 'api_' . $path[0];
                    $values[$this->_controllerKey] = 'api_' . array_shift($path);
                    $values[$this->_actionKey] = 'get';
                } else {
                    // If Controller in URI is not found to be a RESTful
                    // Controller, return false to fall back to other routes
                    return false;
                }
            } elseif ($this->_checkRestfulController($moduleName, $controllerName)) {
                $values[$this->_controllerKey] = $controllerName;
                $values[$this->_actionKey] = 'get';
            } else {
                return false;
            }

            //Store path count for method mapping
            $pathElementCount = count($path);

            // Check for "special get" URI's
            $specialGetTarget = false;
            if ($pathElementCount && array_search($path[0], array('index', 'new')) > -1) {
                $specialGetTarget = array_shift($path);
            } elseif ($pathElementCount && $path[$pathElementCount-1] == 'edit') {
                $specialGetTarget = 'edit';
                $params['id'] = urldecode($path[$pathElementCount-2]);
            } elseif ($pathElementCount == 1) {
                $params['id'] = urldecode(array_shift($path));
            } elseif ($pathElementCount == 0 && !isset($params['id'])) {
                $specialGetTarget = 'index';
            }

            // Digest URI params
            if ($numSegs = count($path)) {
                for ($i = 0; $i < $numSegs; $i = $i + 2) {
                    $key = urldecode($path[$i]);
                    $val = isset($path[$i + 1]) ? $path[$i + 1] : null;
                    $params[$key] = urldecode($val);
                }
            }

            // Determine Action
            $requestMethod = strtolower($request->getMethod());
            if ($requestMethod != 'get') {
                if ($request->getParam('_method')) {
                    $values[$this->_actionKey] = strtolower($request->getParam('_method'));
                } elseif ( $request->getHeader('X-HTTP-Method-Override') ) {
                    $values[$this->_actionKey] = strtolower($request->getHeader('X-HTTP-Method-Override'));
                } else {
                    $values[$this->_actionKey] = $requestMethod;
                }

                // Map PUT and POST to actual create/update actions
                // based on parameter count (posting to resource or collection)
                switch( $values[$this->_actionKey] ){
                    case 'post':
                        if ($pathElementCount > 0) {
                            $values[$this->_actionKey] = 'put';
                        } else {
                            $values[$this->_actionKey] = 'post';
                        }
                        break;
                    case 'put':
                        $values[$this->_actionKey] = 'put';
                        break;
                }

            } elseif ($specialGetTarget) {
                $values[$this->_actionKey] = $specialGetTarget;
            }

        }
        $this->_values = $values + $params;

        $result = $this->_values + $this->_defaults;

        if ($partial && $result)
            $this->setMatchedPath($request->getPathInfo());

        return $result;
    }
}

Module Bootstrap;
class Acl_Bootstrap
    extends Zend_Application_Module_Bootstrap
{
    protected function _initRestRoute()
    {
        $this->bootstrap('frontController');
        $frontController = Zend_Controller_Front::getInstance();

        $restRoute = new Pwb_Controller_Router_Rest(
                $frontController,
                array(),
                array(
                    'acl' => array(
                        'api_role'
                    ))
        );
        $frontController->getRouter()->addRoute('restAcl', $restRoute);
    }

}


class Pwb_Controller_Router_Route_Module extends Zend_Controller_Router_Route_Module
{
    public function match($path, $partial = false)
    {
        $return =  parent::match($path, $partial);
        $front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();
        $is_admin_env = $request->getParam('is_admin_env');
        if($is_admin_env) {
            $return['controller'] = $is_admin_env . '_' .  $return['controller'];
        }
        return $return;
    }
}

class Pwb_Controller_Router_Route extends Zend_Controller_Router_Route
{
    public function match($path, $partial = false) {

        $return = parent::match($path, $partial);
        if(isset($return['admin'])) {
            $front = Zend_Controller_Front::getInstance();
            $request = $front->getRequest();
            $request->setParam('is_admin_env', $return['admin']);
            $front->setRequest($request);
        }
        return $return;
    }
}
于 2011-12-19T10:00:23.447 回答