2013-07-21 07:34
浏览 31

什么是一个好的和简单的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.

图片转代码服务由CSDN问答提供 功能建议

用于创建 RESTFUL API的优秀且简单的 PHP 框架是什么? / code> WebService? 我想做的是创建API,在表上执行CRUD操作,并以 JSON 作为响应。 不仅数据库操作,而且它可能有额外的逻辑添加到代码中。 希望现在问题更清楚了。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpxw17759
    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:


    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

    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.

    点赞 评论