Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

php - How to set up Hierarchical Zend Rest Routes?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...