問題描述
使用 Zend 框架,我嘗試在按以下模式組織的資源上為 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 參數并在 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:
- 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 休息路線?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!