React-Restify
, (*1)
RESTful api made easy for ReactPHP, seriously., (*2)
Instalation
Via composer, (*3)
``` bash
$ composer require capmousse/react-restify, (*4)
## Create server
Here is an exemple of a simple HTTP server replying to all get call like `http://127.0.0.1:1337/hello/you`
``` php
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 :, (*5)
``` php
$server->listen(1337, "[::1]", [
'local_cert' => DIR . 'localhost.pem'
]);, (*6)
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.
``` php
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., (*7)
Next part will be to support Sockets, Upgrade Requests... to create a real time API server., (*8)
Licence
MIT, see LICENCE file, (*9)