使用 Zend_Rest_Controller 提供 REST API 访问时正确的代码结构
我是 Zend Frameworks 和 MVC 类型编程(虽然不是 PHP)的新手,我正在尝试使用 Zend Framework 提供对我的服务器上的数据的 API 访问。
我正在使用 Chris Danielson 的文章 (http://www.chrisdanielson.com/2009/09/02/creating-a-php-rest-api-using-the-zend-framework/)作为基础。
我想提供对以下格式的数据的访问:
(a) http://www.example.com/api/createevent
Data will be POSTed here, success will return an id, otherwise an
error message/code
(b) http://www.example.com/api/geteventdetails/1234
GET request will return data
(c) http://www.example.com/api/getregistrationdetails/1234
GET request will return data
(d) http://www.example.com/api/getregistrationdetails/1234/567
GET request will return data
问题:
有一个默认文件位于 \api\application\controllers\VersionController.php,它可以处理类型为 http://www.example.com/api/version 。我是否应该创建一个单独的文件,位于: \api\application\controllers\GeteventdetailsController.php 来处理类型 (b) 的请求(每种类型的请求都有一个)?如果没有,我应该在哪里放置代码来处理这些多种请求类型?
如何获取(b)至(d)中传递的参数?
要执行请求 (b) 到 (d),我需要从服务器的数据库中获取信息。我应该在哪里放置执行实际 MySQL 查询的代码?
I'm new to Zend Frameworks and MVC type programming (not PHP though) and I'm trying to provide API access to data on my server using the Zend Framework.
I'm using Chris Danielson's article (http://www.chrisdanielson.com/2009/09/02/creating-a-php-rest-api-using-the-zend-framework/) as a base.
I want to provide access to data in the following formats:
(a) http://www.example.com/api/createevent
Data will be POSTed here, success will return an id, otherwise an
error message/code
(b) http://www.example.com/api/geteventdetails/1234
GET request will return data
(c) http://www.example.com/api/getregistrationdetails/1234
GET request will return data
(d) http://www.example.com/api/getregistrationdetails/1234/567
GET request will return data
Questions:
There is a default file which is located at \api\application\controllers\VersionController.php which enables handling of URLs of type: http://www.example.com/api/version . Should I be creating a separate file located at: \api\application\controllers\GeteventdetailsController.php which handles requests of type (b) (and one for every type of request)? If not, where should I be placing my code to handle these multiple request types?
How can I get the parameters passed in (b) to (d)?
To do requests (b) to (d), I need to fetch information from my server's database. Where should I place the code that does the actual MySQL query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 ZF 中使用了很多路由,但没有在 Rest 实现中使用,已经阅读了文档和您链接到的教程 - 我会尽力帮助您...
可能值得查看文档Rest router(大约页面下方的 1/3) - 它解释说它会自动创建路线根据请求的方式为您服务;因此不需要 createevent、geteventdetails 等命名格式。
问题 1.
而不是创建文件
我会创建该文件
这将是一个处理所有事件操作的控制器,无论是获取、发布、放置等。您的建议对于控制器来说过于具体,因为将处理获取、放置等在行动层面。
问题 2.
文档中描述的路由显示最终参数 (:id) 将分配给控制器中名为
id
的参数。因此,使用 GET 访问 URL /events/ 将调用
EventsController.php
文件中的indexAction()
并且使用 GET 访问 URL /events/99/ 将调用
EventsController.php
文件中的getAction()
。您可以像这样从控制器访问此 id,或者
您应该编写代码来查询数据库以获取事件列表或特定 id。这让我们很好地了解...
问题 3。
您应该为数据库表和行创建模型,而不是将用于查询数据库事件的代码放入控制器中。我建议使用 ZF 中的现有设置 Zend_Db_Row 和 Zend_Tb_Table 来执行此操作。这将确保您的应用程序/网站是 MVC。
将代码放入控制器中可能会阻碍以后的开发,例如,当您稍后在另一个控制器中为事件编写注册表单时。创建事件的逻辑将被复制,一次在新控制器中,一次在 Rest 控制器中。您最好将此逻辑集中到用于操作和查询事件的模型中。
我希望这有帮助!
I have used routes a lot in ZF, but not the Rest implementation, having swatted up on the docs and on the tutorial you linked to - I will do my best to help you...
It might be worth looking at the docs for the Rest router (about 1/3 down the page) - it explains that it will automatically create routes for you based on the method of the request; so your naming format of createevent, geteventdetails, etc shouldn't be needed.
Question 1.
Rather than creating the file
I'd create the file
This will be one controller to handle all the event actions, be that getting, posting, putting, etc. Your suggestion is too specific for the controller as the get, put, etc will be handled at the action level.
Question 2.
The routes described in the docs show you that the final parameter (:id) will be assigned to a parameter in the controller called
id
.So accessing the URL /events/ using GET will invoke the
indexAction()
in yourEventsController.php
fileAnd accessing the URL /events/99/ using GET will invoke the
getAction()
in yourEventsController.php
file. You can access this id from the controller like thisOR
You should then write code to query the database for a listing of events or for a specific id. Which brings us nicely on to...
Question 3.
Rather than putting the code for querying the database for events into the controller you should create models for your database tables and rows. I'd recommend using the existing setup in ZF for Zend_Db_Row and Zend_Tb_Table to do this. This will ensure your application/website is MVC.
Putting the code inside the contrller may hinder development later, for example when you write a registation form for an event at a later date in another controller. The logic for creating the event will be duplicated, once in the new controller and once in the Rest controller. You'd be better off centralising this logic into a model for manipulating and querying events.
I hope that helps!