douyi9447 2011-03-16 11:40
浏览 50
已采纳

使用Zend_Rest_Controller提供REST API访问时,正确的代码结构

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:

  1. 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?

  2. How can I get the parameters passed in (b) to (d)?

  3. 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?

  • 写回答

2条回答 默认 最新

  • dpwdsmbvm496180204 2011-03-16 22:31
    关注

    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

    \api\application\controllers\GeteventdetailsController.php

    I'd create the file

    \api\application\controllers\EventsController.php

    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 your EventsController.php file

    And accessing the URL /events/99/ using GET will invoke the getAction() in your EventsController.php file. You can access this id from the controller like this

    $id = $this->getRequest()->getParam("id");
    

    OR

    $id = $this->getRequest()->id;
    

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配