如何设置分层Zend休息路线? [英] How to set up Hierarchical Zend Rest Routes?
问题描述
使用Zend Framework,我试图在以以下模式组织的资源上为REST api构建路由:
- http://example.org/users/
- http://example.org/users/234
- http://example.org/users/234/items
- http://example.org/users/234/items/34
如何使用Zend_Rest_Route进行设置?
这是我在bootstrap.php文件中为用户资源(users/:id)设置路由的方式:
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($frontController);
$frontController->getRouter()->addRoute('default', $restRoute);
[据我所知,这是一条通吃的路线,因此users/324/items/34将导致参数设置为id = 324和items = 34,并且所有内容都将映射到Users(前端模块)模型.从那里,我想我可以测试一下items参数,并在获取请求中为用户#324检索项目#34.]< ===我刚刚检查了一下,它似乎不起作用:>
访问/users/234/items/43和
var_dump($this->_getAllParams());
其余控制器的get操作中的结果将产生以下输出:
array(4) {
["controller"]=> string(5) "users"
["action"]=> string(3) "get"
[2]=> string(5) "items" ["module"]=> string(7) "default"]
}
两个ID都以某种方式迷路了...
有人吗?
AFAIK,Zend_Rest_Route中没有可用于执行此操作的功能.有一个建议,但不确定何时实施.您可以将其添加到Bootstrap中以设置此自定义路由.
$front = $this->getResource('FrontController');
$testRoute = new Zend_Controller_Router_Route(
'users/:user_id/items/:item_id',
array(
'controller' => 'users',
'action' => 'item',
'module' => 'default'
)
);
$front->getRouter()->addRoute('test', $testRoute);
user_id 或 item_id 可以在UsersController的itemAction中用作参数:
$user_id = $this->_request->getParam('user_id');
With the Zend Framework, I am trying to build routes for a REST api on resources organized in the following pattern:
- http://example.org/users/
- http://example.org/users/234
- http://example.org/users/234/items
- http://example.org/users/234/items/34
How do I set up this with Zend_Rest_Route?
Here is how I have setup the route for the users resource (users/:id) in my bootstrap.php file:
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($frontController);
$frontController->getRouter()->addRoute('default', $restRoute);
[As far as I understand, this is a catch all route so users/324/items/34 would results in parameters set as id=324 and items=34 and everything would be mapped to the Users (front module) Model. From there I guess I could just test for the items parameter and retrieve the item #34 for user #324 on a get request.]<=== I just checked it and it doesn't seems to work like that:
Acessing /users/234/items/43 and
var_dump($this->_getAllParams());
in the get action of the rest controller results in the following output:
array(4) {
["controller"]=> string(5) "users"
["action"]=> string(3) "get"
[2]=> string(5) "items" ["module"]=> string(7) "default"]
}
Somehow both ids got lost...
Anyone?
AFAIK, there is no feature in Zend_Rest_Route which allows you to do something like this. There is a proposal but not sure when it'll be implemented. You can add this in your Bootstrap to set up this custom route.
$front = $this->getResource('FrontController');
$testRoute = new Zend_Controller_Router_Route(
'users/:user_id/items/:item_id',
array(
'controller' => 'users',
'action' => 'item',
'module' => 'default'
)
);
$front->getRouter()->addRoute('test', $testRoute);
user_id or item_id become available in itemAction in UsersController as parameters:
$user_id = $this->_request->getParam('user_id');
这篇关于如何设置分层Zend休息路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!