1

我正在使用 Zend Framework 1.12.3 开发一个 API。我正在使用 Zend_Rest_Route,但我想要分层 URL:

我正在考虑使用这种方法,因为我必须将某些科目分配给某些教授,而且我相信这种模式可以巧妙地解决它。

但是,我很难实现分层 URL。我已经尝试过:

  1. Zend_Controller_Router_Route with Chains,在 config .ini 文件中,但由于必须指定控制器和动作,当访问http://api.example.com/professors/:professorId/subjects时,它总是指向相同的动作(即,无论调用方法是什么——POST、PUT、GET、DELETE——它总是指向配置.ini 文件中指定的操作)。例如,如果我在配置文件中指定了 getAction,使用链它总是会调用 getAction,无论我使用的是什么方法。目前,当进行 POST 调用时,它实际上调用了 postAction()(类似地发生在 PUT、GET、DELETE、PATCH、HEAD 和 OPTIONS 中)。我的控制器文件如下所示:

    class V1_ProfessorsController extends REST_Controller
    {
            public function optionsAction()
            {
                    // code goes here
            }
    
            public function headAction()
            {
                    // code goes here
            }
    
            public function indexAction()
            {
                    // code goes here - list of resources
            }
    
            public function getAction()
            {
                    // code goes here
            }
    
            public function postAction()
            {
                    // code goes here
            }
    
            public function putAction()
            {
                    // code goes here
            }
    
            public function patchAction()
            {
                    // code goes here
            }
    
            public function deleteAction()
            {
                    // code goes here
            }
    
    }
    
  2. 继承 Zend_Rest_Route 并覆盖 match() 函数,如此处所指出。问题是,虽然这在调用时确实有效http://api.example.com/professors/:professorId/subjects,但它仍然使用调用时使用的相同的 ProfessorsController http://api.example.com/professors。我不确定这一点,但我相信最好有自己的控制器(例如ProfessorSubjectsController)。

另外,我有一个问题。分层路由应该如何工作?为不同的资源/子资源设置不同的控制器会更好吗?例如,有 ProfessorsController forhttp://api.example.com/professors/:professorId和 ProfesSubjectsController for http://api.example.com/professors/:professorId/subjects/:subjectId?

4

1 回答 1

3

我在某个地方找到了一个稍微修改过的解决方案。这是一个自定义路由类,我认为我们都希望它做。

<?php 

require_once "modules.inc";

class Rest_Controller_Route extends Zend_Controller_Router_Route
{

/**
 * @var Zend_Controller_Front
 */
protected $_front;

protected $_actionKey     = 'action';

/**
 * Prepares the route for mapping by splitting (exploding) it
 * to a corresponding atomic parts. These parts are assigned
 * a position which is later used for matching and preparing values.
 *
 * @param Zend_Controller_Front $front Front Controller object
 * @param string $route Map used to match with later submitted URL path
 * @param array $defaults Defaults for map variables with keys as variable names
 * @param array $reqs Regular expression requirements for variables (keys as variable names)
 * @param Zend_Translate $translator Translator to use for this instance
 */
public function __construct(Zend_Controller_Front $front, $route, $defaults = array(), $reqs = array(), Zend_Translate $translator = null, $locale = null)
{
    $this->_front      = $front;
    $this->_dispatcher = $front->getDispatcher();

    parent::__construct($route, $defaults, $reqs, $translator, $locale);
}



/**
 * Matches a user submitted path with parts defined by a map. Assigns and
 * returns an array of variables on a successful match.
 *
 * @param string $path Path used to match against this routing map
 * @return array|false An array of assigned values or a false on a mismatch
 */
public function match($path, $partial = false)
{

    $return = parent::match($path, $partial);

    // add the RESTful action mapping
    if ($return) {
        $request = $this->_front->getRequest();
        $path   = $request->getPathInfo();
        $params = $request->getParams();

        $path   = trim($path, '/');

        if ($path != '') {
            $path = explode('/', $path);
        }

        $lastParam = array_pop($path);

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

            // Map PUT, DELETE and POST to actual create/update/delete actions
            // based on parameter count (posting to resource or collection)
            switch( $return[$this->_actionKey] ){
                case 'post':
                    $return[$this->_actionKey] = 'post';
                    break;
                case 'put':
                    $return[$this->_actionKey] = 'put';
                    $return["id"] = $lastParam;
                    break;
                case 'delete':
                    $return[$this->_actionKey] = 'delete';
                    $return["id"] = $lastParam;
                    break;
            }
        } else {
            // if the last argument in the path is a numeric value, consider this request a GET of an item
            if (is_numeric($lastParam)) {
                $return[$this->_actionKey] = 'get';
                $return["id"] = $lastParam;
            } else {
                if (isset($data[0]) && is_numeric($data[0])) {
                    $return[$this->_actionKey] = 'get';
                    $return["id"] = $lastParam;
                } else {
                    $return[$this->_actionKey] = 'index';
                }
            }
        }
    }

    return $return;

}

}

要使用它,请在 bootstrap 或 index.php 中像这样创建所有路由,两个示例:

$route = new Rest_Controller_Route($front, 'customers/*', array('controller' => 'customers'));
$router->addRoute('customers', $route);

$route = new Rest_Controller_Route($front, 'customers/:customer_id/documents/*', array('controller' => 'customers-documents'));
$router->addRoute('customersdocuments', $route);

这对我来说是一种魅力。你,考虑到这不是我的最终解决方案,所以可能有我没有发现的龙,所以要注意。:)

于 2013-06-05T06:20:42.747 回答