Simple PHP Library for RESTful APIs
Go to file
PJ Dietz 972f8e9c26 Update Request and Response
Add constructor for Request to provide URI and method on instantiation.
Add success property and getSuccess() method Response.

Closes #1
2013-03-26 21:05:36 -04:00
src/pjdietz/WellRESTed Update Request and Response 2013-03-26 21:05:36 -04:00
.gitignore Update README.md 2013-01-30 20:18:07 -05:00
README.md Add license to README 2013-02-01 21:38:23 -05:00
composer.json Add license to composer.json 2013-02-02 10:24:05 -05:00

README.md

WellRESTed

WellRESTed provides classes to help you create RESTful APIs and work with HTTP requests and responses.

Requirements

Install

Add an entry for "pjdietz/wellrested" to your composer.json file's require property. If you are not already using Composer, create a file in your project called composer.json with the following content:

{
    "require": {
        "pjdietz/wellrested": "1.*"
    }
}

Use Composer to download and install WellRESTed. Run these commands from the directory containing the composer.json file.

$ curl -s https://getcomposer.org/installer | php
$ php composer.phar install

You can now use WellRESTed by including the autoload.php file generated by Composer. (vendor/autoload.php)

Examples

Responses

Use a Response instance to build a response and send output. The Response class makes it easy to set the status code, add headers, and set the response body.

$resp = new \pjdietz\WellRESTed\Response();
$resp->statusCode = 200;
$resp->setHeader('Content-type', 'text/plain');
$resp->body = 'Hello world!';
$resp->respond();
exit;

Requests

Use the Request class to read information about the request sent to the server. You can read the headers, method, and message body.

Request provides a singleton instance representing the request sent to the current script.

$rqst = \pjdietz\WellRESTed\Request::getRequest();

if ($rqst->method === 'PUT') {
    $obj = json_decode($rqst->body);
    // Do something with the JSON sent as the message body.
}

The Request class can also make a request to another server and provide the response as a Respones object. (This feature requires PHP cURL.)

// Prepare a request.
$rqst = new \pjdietz\WellRESTed\Request();
$rqst->uri = 'http://my.api.local/resources/';
$rqst->method = 'POST';
$rqst->body = json_encode($newResource);

// Make the request.
$resp = $rqst->request();

// Read the response.
if ($resp->statusCode === 201) {
    // The new resource was created.
    $newResource = json_decode($resp->body);
}

URIs and Routing

WellRESTed also provides several classes to facilitate working with resource-based URIs. You can create your own regular expressions to match the URIs, or you can use URI templates.

Here's an example of a Router subclass. The subclass examines a request URI, compares it against a series of URI templates, and matches the request to a particular Handler class.

For more information on URI templates, see RFC 6570.

/**
 * Loads and instantiates handlers based on URI.
 */
class MyRouter extends \pjdietz\WellRESTed\Router
{
    public function __construct()
    {
        parent::__construct();

        // Match any request to the URI "/things/"
        // Send it to a handler for collections of thing objects.
        $this->addRoute(Route::newFromUriTemplate('/things/', 'ThingCollectionHandler'));

        // Match any request to "/things/" followed by a variable.
        // Send the request to a handler for one thing object.
        // The ThingItemHandler will receive an array containing an "id" key
        // and the value from the URI.
        $this->addRoute(Route::newFromUriTemplate('/things/{id}', 'ThingItemHandler'));

        // ...Add as manu routes as you like here...
    }
}

More Examples

For more examples, see the project pjdietz/wellrested-samples.

Copyright © 2013 by PJ Dietz Licensed under the MIT license