Zend_Rest_Route 和分层路由 [英] Zend_Rest_Route and hierarchical routes
问题描述
使用 Zend 1.9,我尝试使用 Zend_Rest_Route
来实现分层 url.所以我有 2 个 restFul 控制器(users
和 items
)并使用分层路由调用第二个控制器.
Using Zend 1.9, I am trying to use Zend_Rest_Route
to implement hierarchical url. So I have 2 restFul controller (users
and items
) and invoke the second controller using hierarchical routes.
示例:(考虑 GET http 动词)
Example: (consider GET http verb)
- http://example.org/users///这个指向
indexAction
usersController
的代码> - http://example.org/users/234//这个指向
getAction
usersController
的 - http://example.org/users/234/items//这个指向
itemsController
的 - http://example.org/users/234/items/34//this 指向
itemsController
的
indexAction
showAction
- http://example.org/users/ //this point to
indexAction
ofusersController
- http://example.org/users/234 //this point to
getAction
ofusersController
- http://example.org/users/234/items //this point to
indexAction
ofitemsController
- http://example.org/users/234/items/34 //this point to
showAction
ofitemsController
示例:(考虑 POST http 动词)
Example: (consider POST http verb)
- http://example.org/users///这个指向
postAction
usersController
的代码> - http://example.org/users/234/items//这个指向
itemsController
的
postAction
- http://example.org/users/ //this point to
postAction
ofusersController
- http://example.org/users/234/items //this point to
postAction
ofitemsController
示例:(考虑 PUT http 动词)
Example: (consider PUT http verb)
- http://example.org/users/234//这个指向
putAction
usersController
的 - http://example.org/users/234/items/34//this 指向
itemsController
的
putAction
- http://example.org/users/234 //this point to
putAction
ofusersController
- http://example.org/users/234/items/34 //this point to
putAction
ofitemsController
示例:(考虑 DELETE http 动词)
Example: (consider DELETE http verb)
- http://example.org/users/234//这个指向
deleteAction
usersController
的 - http://example.org/users/234/items/34//this 指向
itemsController
的
deleteAction
- http://example.org/users/234 //this point to
deleteAction
ofusersController
- http://example.org/users/234/items/34 //this point to
deleteAction
ofitemsController
Zend Framework 似乎没有提供开箱即用的功能,所以我尝试实现我的自定义解决方案,但我不是很满意.
It seems Zend Framework don't provide this feature out of the box, so I have try to implement my custom solution but I am not very satisfied.
$front = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($front, [], [
'moduleName' => [
'users'
]
]);
$front->getRouter()->addRoute('users', $restRoute);
$route = new Zend_Controller_Router_Route('moduleName/users/:user_id/items', [
'controller' => 'items',
'module' => 'moduleName',
'action' => 'generic'
]);
$front->getRouter()->addRoute('items_generic', $route);
$route = new Zend_Controller_Router_Route('moduleName/users/:user_id/items/:item_id', [
'controller' => 'items',
'module' => 'moduleName',
'action' => 'specific'
]);
$front->getRouter()->addRoute('items_specific', $route);
这是itemsController.php
的原型:
class ModuleName_ItemsController extends Zend_Controller_Action
{
public function genericAction () //called from http://example.org/users/234/items
{
if ($this->getRequest()->isGet()) {
$this->privateindex();
} else if ($this->getRequest()->isPost()){
$this->privatepost();
}
}
public function specificAction () //called from http://example.org/users/234/items/34
{
if ($this->getRequest()->isGet()) {
$this->privateshow();
} else if ($this->getRequest()->isPut() ||$this->getRequest()->isPost()){
$this->privateput();
}else if($this->getRequest()->isDelete()){
$this->privatedelete();
}
}
private function privateindex(){ return $this->_helper->json->sendJson([
'items' => 'indexPrivata'
]);}
private function privatepost(){ return $this->_helper->json->sendJson([
'items' => 'postPrivata'
]);}
private function privateshow(){ return $this->_helper->json->sendJson([
'items' => 'showPrivata'
]);}
private function privateput(){ return $this->_helper->json->sendJson([
'items' => 'putPrivata'
]);}
private function privatedelete(){ return $this->_helper->json->sendJson([
'items' => 'deletePrivata'
]);}
}
这个解决方案似乎有效,但在我看来并不是最好的方法.
This solution seems work but in my opinion is not the best way to do this.
在 Zend 中实现分层 restFul 路由有更好的解决方案吗?
There is a better solution to implement hierarchical restFul routes in Zend?
推荐答案
您可以使用插件管理路由.
You can manage route with a plugin.
例如,您可以尝试这样的操作(未使用 ZF 1.9 进行测试):
For example, you can try something like this (not tested with ZF 1.9):
在你的bootstrap
中,添加这个函数(声明插件)
In your bootstrap
, add this function (to declare the plugin)
public function _initPlugins(){
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Application_Plugin_PRoutage());
}
在这个例子中,application/plugins
文件夹,像这样创建 PRoutage.php
插件:
with this example, the application/plugins
folder, create the PRoutage.php
plugin like this:
class Application_Plugin_PRoutage extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
if (preg_match('/(users)(.*)(items)/', $request->getPathInfo())){
$request->setControllerName('items'); // itemsController
if ($request->isGet()){
if (preg_match('/(users\/)(.*)(items\/)([0-9].)/', $request->getPathInfo())){
// http://example.org/users/234/items/34 //this point to showAction of itemsController
$request->setActionName('show');
}
else{
// http://example.org/users/234/items //this point to indexAction of itemsController
$request->setActionName('index');
}
} elseif ($request->isPost()){
// http://example.org/users/234/items //this point to postAction of itemsController
$request->setActionName('post');
} elseif ($request->isPut()){
// http://example.org/users/234/items/34 //this point to putAction of itemsController
$request->setActionName('put');
}elseif ($request->isDelete()){
// http://example.org/users/234/items/34 //this point to deleteAction of itemsController
$request->setActionName('delete');
}
$request->setDispatched(true) ;
}
elseif (preg_match('/(users)/', $request->getPathInfo())){
$request->setControllerName('users'); // usersController
if ($request->isGet()){
if (preg_match('/(users\/)([0-9].)/', $request->getPathInfo())){
// http://example.org/users/234 //this point to getAction of usersController
$request->setActionName('get');
}
else{
// http://example.org/users/ //this point to indexAction of usersController
$request->setActionName('index');
}
} elseif ($request->isPost()){
// http://example.org/users/ //this point to postAction of usersController
$request->setActionName('post');
} elseif ($request->isPut()){
// http://example.org/users/234 //this point to putAction of usersController
$request->setActionName('put');
}elseif ($request->isDelete()){
// http://example.org/users/234 //this point to deleteAction of usersController
$request->setActionName('delete');
}
$request->setDispatched(true) ;
}
}
}
当然,你可以改进它.
希望能帮到你.
这篇关于Zend_Rest_Route 和分层路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!