dongyuzhu2244 2013-07-21 07:34
浏览 41
已采纳

什么是一个好的和简单的PHP框架来创建一个RESTFUL API WebService [关闭]

What is a good and easy PHP framework to create a RESTFUL API WebService? What I want to do, is to create API the performs CRUD operations on the table and aims to make a response as JSON. Not only database operations, but also it may have extra logic to add in the code. Hope the question is more clear now.

  • 写回答

1条回答 默认 最新

  • dpxw17759 2013-07-21 07:40
    关注

    Answer edited based upon updated question

    There is no mature framework that I know of which does a RESTful API with JSON responses out of the box. It is largely up to you as the coder to implement those details.

    I would honestly pick a well documented framework like Codeigniter, and go from there. Your API endpoints will be whatever you define in either /application/config/routes.php, or based on your controller naming convention. Remember that the key to a RESTful webservice is that it's stateless. Your URLs should be something like:

    /products/381
    /users/812
    /tutorials/19
    

    You don't want /tutorials/updatetutorial/19 (that is not restful).

    In strict terms, you will want your controllers to listen for HTTP request methods on certain URLs. For example, say the endpoint for a given product is at

    http://somedomain.com/api/products/381

    You will then want to use HTTP request methods to determine what you are doing to that endpoint:

    POST to that URL means you are updating it/editing it GET to that URL means you are simply requesting it DELETE to that URL means you want to delete the resource

    Don't do things like /products/381/delete or products/381/edit. That is not strictly RESTful. Your URLs should NEVER contain verbs.

    So what you do is simply structure your controller methods to listen for those HTTP request methods, and call CRUD operations from your models accordingly. To respond with JSON, it's quite easy: just take any object or array from a database query, and echo json_encode($data) it.

    Remember to use the php header() function to create headers that indicate the appropriate responses for your API. You will probably want to create a class explicitly for outputting your query results in JSON with the right headers, just so you don't have to repeat yourself a lot.

    But you'll also want to secure your API, which is a whole other topic that is out of the scope of what I can answer here. If you search StackOverflow or google, you'll find a whole bunch of topics about securing an API. Do your research though, it's quite a big subject.


    Well the answer to that I think depends on your understanding of RESTful, and what you ultimately want to use it for. Is it meant to be an API? What kind of webservice is it going to be?

    The short answer is that there isn't really a PHP Framework built explicitly to be a RESTful web service. RESTfulness is all in how you choose to implement it, structure your database, and structure your URLs.

    Personally I like working with Codeigniter, and I have built a RESTful API webservice with it before, but I had to code the API part of it myself - CI didn't have anything explicitly built in.

    I've played around with Epiphany framework before, which is very much API-focused, but it does things a little oddly and I didn't explore it far enough to know.

    But at the end of the day, a RESTful implementation really depends on you as the coder, and what your ultimately goals are. So perhaps if you update your post with some more details, we can better point you in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记