Coder Social home page Coder Social logo

wellrested's Introduction

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 and License

Copyright © 2015 by PJ Dietz Licensed under the MIT license

wellrested's People

Contributors

pjdietz avatar eatonphil avatar nthdesign avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.