Giter Site home page Giter Site logo

r7's Introduction

R7

Incredibly small multi-purpose RESTful PHP framework. The main idea behind R7 was to create the smallest, and fastest PHP framework designed to handle RESTful calls.

License

The package is licensed under the permissive New BSD license. You may use it in open-source and in commercial products equally. For more information read LICENSE.md

Design

The R7 is based around Routes and Responses. Every route is essentially a URL path that can have Rails like URL parameters. Routes can be created by using either creating a new object or using the create method

// the same as $route = new \R7\Route("/index/test/:name/:value");
$route = \R7\Route::create("/index/test/:name/:value");

Routes can have methods bound. Every HTTP method type is a class method which takes either a callable closure or a class as a parameter. When you specify a class, that class must have a main function with one parameter. The closure is also called with one parameter. The parameter contains the URL parameters.

// called on GET
$route->get(function($req, $data) {
    // $data["name"] and $data["value"] is set
});

class PostHandler {
    function main($req, $data) {
        // $data["name"] and $data["value"] is set
    }
}

// called on POST
$route->post(new PostHandler);

For every HTTP method there can be more than one method bound. But it is important that if you return a response from the method, only the last response will be used, the others will be ignored

Responses

Responses are simple key-value datasets with http status information.

$route->get(function($req, $data) {
    // return everything from the URL parameters
    return new \R7\Response(200, $data); // HTTP status 200
});

Responses can be returned to client as JSON, XML or CSV data. The R7 class uses the ?type=<type> URL query string to choose from the data format. When type is not defined, JSON is automatically used. Calling the above example with the URL http://example.com/index/test/voov/10?type=json returns

{'name':'voov','value':'10'}

while calling it with http://example.com/index/test/voov/10?type=xml returns

<root>
   <name>voov</name>
   <value>10</value>
</root>

Requests

Request objects are handed to the controller function as the first parameter. You can access the GET, POST, PUT or DELETE parameters from here, and also verify a signed request.

To create a signed request:

/* $dataArray is a key-value associative array of the data
 * The function returns a key1=value1&key2=value2&signature=XXXXXXXXXX
 * representation of the $dataArray
 */
\R7\Request::makeRequest($dataArray, file_get_contents("/path/to/private.key.pem"));

You can simply verify a signed request in a Route:

$route->get(function($req, $data) {
    if(!$req->verifySignature(file_get_contents("/path/to/public.key")) {
        return new \R7\Response(403, array("status" => "forbidden")); // return 403 Forbidden
    }
    return new \R7\Response(200, $data); // HTTP status 200
});

r7's People

Stargazers

 avatar

Watchers

 avatar  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.