Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

CapMousse/React-Restify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React-Restify

Scrutinizer Code Quality

RESTful api made easy for ReactPHP, seriously.

Instalation

Via composer

    $ composer require capmousse/react-restify

Create server

Here is an exemple of a simple HTTP server replying to all get call like http:https://127.0.0.1:1337/hello/you

require 'vendor/autoload.php';

$server = new CapMousse\ReactRestify\Server("MyAPP", "0.0.0.1");

// Middleware
$server->use(function ($request, $next) {
	print_r($request->getMethod());
	$next();
});

// Dependency injection
$server->add(\Foo\Bar::class)

$server->get('/hello/{name}', function ($request, $response, \Foo\Bar $bar, $name) {
    $response
    	->write("Hello {$name}")
    	->end();

    $bar->foobar();
});

$server->listen(1337);

To create a secure HTTPS server, you need to give all your cert files to the server :

$server->listen(1337, "[::1]", [
    'local_cert' => __DIR__ . 'localhost.pem'
]);

More examples can be found on the example directory like the Todo example.

Controller, Middleware and Dependency injection

React-Restify support Controller call, middleware à la express and Dependency Injection.

use CapMousse\ReactRestify\Http\Request;
use CapMousse\ReactRestify\Http\Response;

class Foo {
    public function bar() {
        echo "Do something";
    }
}

class FooBar {
    public function baz (Response $response, Foo $foo) {
        $foo->bar();
        $response->end()
    }
}

$server->add(Foo::class);

$server->use(function ($request, $next) {
    echo $request->httpRequest->getPath();
});

$server->get('/', 'FooBar@baz');

Design goals

React-Restify was primary made to build RESTful api easily. It can be used like Silex or Express.

Next part will be to support Sockets, Upgrade Requests... to create a real time API server.

Licence

MIT, see LICENCE file