如何设置分层 Zend 休息路由?

发布于 2024-08-20 20:56:40 字数 1361 浏览 6 评论 0原文

使用 Zend Framework,我尝试在按以下模式组织的资源上构建 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 参数并在获取请求上检索用户 #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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风也等你 2024-08-27 20:56:40

我将解决方案开源到 github 中并作为作曲家包。请参阅https://github.com/aporat/Application_Rest_Controller_Route

我添加了一个新类,它扩展了 Zend_Controller_Router_Route 并添加了添加自定义静态路由的能力,例如

$frontController = Zend_Controller_Front::getInstance();
$frontController->getRouter()->addRoute('users-messages', new Application_Rest_Controller_Route($frontController, 'users/:user_id/messages', ['controller' => 'users-messages']));

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

$frontController = Zend_Controller_Front::getInstance();
$frontController->getRouter()->addRoute('users-messages', new Application_Rest_Controller_Route($frontController, 'users/:user_id/messages', ['controller' => 'users-messages']));
香草可樂 2024-08-27 20:56:40

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_iditem_id 在 UsersController 的 itemAction 中可以作为参数使用:

$user_id = $this->_request->getParam('user_id');

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');
巡山小妖精 2024-08-27 20:56:40

仅当有 1 个参数时,Zend_Rest_Route 将控制器名称后面的第一个参数映射到“id”。

我想出的最佳解决方案是子类化 Zend_Rest_Route 并覆盖 match() 函数。复制 Zend_Rest_Route 的 match 函数,但在“Digest URI Params”注释之前添加以下内容(大约 60 行)。

if($pathElementCount > 1 && $path[0] != 'id') {
    $params['id'] = urldecode($path[0]);
    array_shift($path);
}  

这应该会给你你正在寻找的功能。

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).

if($pathElementCount > 1 && $path[0] != 'id') {
    $params['id'] = urldecode($path[0]);
    array_shift($path);
}  

That should give you the functionality you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文