用于CodeIgniter的一个完整RESTful服务器实现:CodeIgniter Rest Server

jopen 9年前

用于CodeIgniter的一个完整RESTful服务器实现,只用到一个库,一个配置文件和一个控制器。

Requirements

  1. PHP 5.4 or greater
  2. CodeIgniter 3.0+

Note: for 1.7.x support download v2.2 from Downloads tab

Installation

Drag and drop the application/libraries/Format.php and application/libraries/REST_Controller.php files into your application's directories. To userequire_onceit at the top of your controllers to load it into the scope. Additionally, copy the rest.php file from application/config in your application's configuration directory.

Handling Requests

When your controller extends fromREST_Controller, the method names will be appended with the HTTP method used to access the request. If you're making an HTTPGETcall to/books, for instance, it would call aBooks#index_get()method.

This allows you to implement a RESTful interface easily:

class Books extends REST_Controller  {    public function index_get()    {      // Display all books    }      public function index_post()    {      // Create a new book    }  }

REST_Controlleralso supportsPUTandDELETEmethods, allowing you to support a truly RESTful interface.

Accessing parameters is also easy. Simply use the name of the HTTP verb as a method:

$this->get('blah'); // GET param  $this->post('blah'); // POST param  $this->put('blah'); // PUT param

The HTTP spec for DELETE requests precludes the use of parameters. For delete requests, you can add items to the URL
public function index_delete($id)  {      $this->response([          'returned from delete:' => $id,      ]);  }

If query parameters are passed via the URL, regardless of whether it's a GET request, can be obtained by the query method:

$this->query('blah'); // Query param

项目主页:http://www.open-open.com/lib/view/home/1442241855618