使用 Zend 框架,我尝试在按以下模式组织的资源上为 REST api 构建路由:
我如何使用 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 参数并在 get 请求中为用户 #324 检索项目 #34.]<=== 我刚刚检查了它,它似乎不是这样工作的:>
访问/users/234/items/43 和
var_dump($this->_getAllParams());
在其余控制器的 get 操作中产生以下输出:
array(4) {[控制器"]=>字符串(5)用户"[动作"]=>字符串(3)获取"[2]=>string(5) "items" ["module"]=>字符串(7)默认"]}
不知何故,两个 ID 都丢失了...
有人吗?
AFAIK,Zend_Rest_Route 中没有允许您执行此类操作的功能.有一个提案,但不确定何时实施.您可以将其添加到 Bootstrap 中以设置此自定义路由.
$front = $this->getResource('FrontController');$testRoute = new Zend_Controller_Router_Route('users/:user_id/items/:item_id',大批('控制器' =>'用户','动作' =>'物品','模块' =>'默认'));$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:
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 休息路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!