RESTful api made easy for ReactPHP, seriously.
Via composer
$ composer require capmousse/react-restify
Here is an exemple of a simple HTTP server replying to all get call like 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.
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');
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.
MIT, see LICENCE file