charm / dispatcher
Creates PSR-7 Server Server Requests for PSR-15 Server Request Handlers and Middlewares from a web server, ReactPHP, Swoole or the command line.
0.0.9
2021-07-16 15:10 UTC
Requires
- charm/error: ^0
- nyholm/psr7: ^1.4
- nyholm/psr7-server: ^1.0
- psr/http-factory: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- f2/asserty: ^1.0
README
Create an application that implements Psr\Http\Server\RequestHandlerInterface
, and run it:
<?php
use Psr\Http\Server\RequestHandlerInterface;
use function Charm\serve;
class HelloWorldApp implements RequestHandlerInterface {
/**
* Responds with "Hello World from /whatever/path"
*/
public function handle(ServerRequestInterface $request): ResponseInterface {
return $this->createResponse(200)->withBody($this->createStream("Hello World from ".$request->getRequestTarget()));
}
}
// Serve via HTTP or command line
serve(new MyApp());
Now you can access your application via the web and from the command line:
# php myapp.php
Hello World from /
# php myapp.php users 123
Hello World from /users/123
# php myapp.php users --email "you@example.com"
Hello World from /users?email=you@example.com
# curl https://myserver.php/users?email=you@example.com
Hello World from /users?email=you@example.com