A RESTful PHP micro-framework for web applications and APIs.
- Automatic Routing - Comforter can search and register service classes for routing automatically. Disable to register services manually.
- Custom Encoders - Set encoders to handle any format request.
- Custom HTTP Request Methods - Add non standard verbs for Comforter to route to your services.
- Gzip Compression
PHP >= 5.3.0
- Add the .htaccess file included in this repository. This file redirects all incoming requests to a single dispatcher.
- Create a dispatcher file (i.e. index.php) that includes Comforter.
- Create a class that has the word "Service" as a suffix (UserService, SearchService, etc.).
- Create a method in that class with a name starting with an HTTP Request Method verb like get, post, put or delete (i.e. getUser, postUser, putUser, deleteUser, etc.). The method must return a PHP type, object or array.
- Include the Service class in the dispatcher (this is done automatically by default).
- Start Comforter.
<?php
// user.php
class UserService {
public static function getName($context) {
return array("first" => "John", "last" => "Doe");
}
}
<?php
// index.php
require "comforter.php";
require "user.php";
\Comforter\Api::Start();
- Comforter can create routes automatically. Thus,
/userprofile/
=>UserProfileService
andGET /userprofile/userdata/
=>UserProfileService::getUserData()
- All methods within the Service class must be static.
- All methods within the Service class must contain a $context argument, which contains an array with three keys:
- request contains $_REQUEST data
- args contains all of the slugs in the URL beyond the second slash
- headers contains all the HTTP Headers that were sent along with the request
- All methods must start with an HTTP Request Method verb, like get, post, put, delete, options, etc.
- The 'Accept' header defines the API method's output. If Accept is 'application/json', it'll automatically encode the data.
Comforter is licensed under the MIT License. Comforter is forked from Descanse.