如何设置分层 Zend 休息路由?
使用 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,所有内容都将映射到用户(前端模块)模型。从那里我想我可以只测试 items 参数并在获取请求上检索用户 #324 的项目 #34。]<=== 我刚刚检查了它,它似乎并不像那样工作:
Acessing / 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 都丢失了...
有人吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将解决方案开源到 github 中并作为作曲家包。请参阅https://github.com/aporat/Application_Rest_Controller_Route。
我添加了一个新类,它扩展了 Zend_Controller_Router_Route 并添加了添加自定义静态路由的能力,例如
I open sourced the solution into github and as a composer package. see https://github.com/aporat/Application_Rest_Controller_Route.
I added a new class which extends Zend_Controller_Router_Route and adds abiliy to add custom restful routes, such as
AFAIK,Zend_Rest_Route 中没有任何功能允许您执行类似的操作。有一个提案,但不确定何时实施。您可以将其添加到 Bootstrap 中以设置此自定义路由。
user_id 或 item_id 在 UsersController 的 itemAction 中可以作为参数使用:
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.
user_id or item_id become available in itemAction in UsersController as parameters:
仅当有 1 个参数时,Zend_Rest_Route 将控制器名称后面的第一个参数映射到“id”。
我想出的最佳解决方案是子类化 Zend_Rest_Route 并覆盖 match() 函数。复制 Zend_Rest_Route 的 match 函数,但在“Digest URI Params”注释之前添加以下内容(大约 60 行)。
这应该会给你你正在寻找的功能。
Zend_Rest_Route maps the first parameter after the controller name to 'id' only when there is 1 parameter.
Best solution I've come up with is to subclass Zend_Rest_Route and override the match() function. Copy Zend_Rest_Route's match function, but add the following just before the "Digest URI Params" comment (about 60 lines in).
That should give you the functionality you're looking for.