Simple PHP Library for RESTful APIs
Go to file
PJ Dietz 50f1004be5 Test cleanup 2017-08-03 14:29:54 -04:00
docker Setup Docker 2017-07-22 14:39:10 -04:00
docs Add docs for additional components 2015-06-13 18:43:36 -04:00
src Minor refactor of Router; various cleanup 2017-08-03 14:15:08 -04:00
test Test cleanup 2017-08-03 14:29:54 -04:00
vagrant Update Vagrant PHP to v5.6 2016-05-18 20:26:53 -04:00
.gitattributes Add .gitattributes to remove non-essentials files from dist 2015-05-15 19:27:18 -04:00
.gitignore Clean user files from .gitignore 2016-05-18 20:43:39 -04:00
.travis.yml Update Travis to test using PHP 7.1 to meet dependency requirements for tests. 2017-08-03 14:09:40 -04:00
README.md Update README docs badge to link to documentation page 2015-05-25 11:20:26 -04:00
Vagrantfile Read Vagrant host port from environment variable. 2015-03-11 15:36:49 -04:00
composer.json Upgrade PHPUnit to ^6 2017-07-22 15:21:41 -04:00
composer.lock Upgrade PHPUnit to ^6 2017-07-22 15:21:41 -04:00
docker-compose.yml Setup Docker 2017-07-22 14:39:10 -04:00
phpunit.xml.dist Update PHPUnit to v5 2016-05-18 20:32:55 -04:00

README.md

WellRESTed

Build Status Documentation Status SensioLabsInsight

WellRESTed is a library for creating RESTful Web services in PHP.

Requirements

  • PHP 5.4

Install

Add an entry for "wellrested/wellrested" to your composer.json file's require property.

{
    "require": {
        "wellrested/wellrested": "~3.0"
    }
}

Documentation

See the documentation to get started.

Example

<?php

use WellRESTed\Message\Stream;
use WellRESTed\Server;

require_once "vendor/autoload.php";

// Build some middleware. We'll register these with a server below.
// We're using callables to fit this all in one example, but these
// could also be classes implementing WellRESTed\MiddlewareInterface.

// Set the status code and provide the greeting as the response body.
$hello = function ($request, $response, $next) {

    // Check for a "name" attribute which may have been provided as a
    // path variable. Use "world" as a default.
    $name = $request->getAttribute("name", "world");

    // Set the response body to the greeting and the status code to 200 OK.
    $response = $response->withStatus(200)
        ->withHeader("Content-type", "text/plain")
        ->withBody(new Stream("Hello, $name!"));

    // Propagate to the next middleware, if any, and return the response.
    return $next($request, $response);

};

// Add a header to the response.
$headerAdder = function ($request, $response, $next) {
    // Add the header.
    $response = $response->withHeader("X-example", "hello world");
    // Propagate to the next middleware, if any, and return the response.
    return $next($request, $response);
};

// Create a server
$server = new Server();

// Start each request-response cycle by dispatching the header adder.
$server->add($headerAdder);

// The header adder will propagate to this router, which will dispatch the
// $hello middleware, possibly with a {name} variable.
$server->add($server->createRouter()
    ->register("GET", "/hello", $hello)
    ->register("GET", "/hello/{name}", $hello)
);

// Read the request from the client, dispatch middleware, and output.
$server->respond();

Copyright © 2015 by PJ Dietz Licensed under the MIT license