fatcode / http-server
Fast, reliable, psr-15, psr-7 compatible php http server.
Requires
- php: >=7.2.0
- ext-json: *
- ext-simplexml: *
- ext-zlib: *
- fatcode/enum: >=1.1
- nikic/fast-route: >=1.3
- psr/http-message: >=1.0
- psr/http-server-middleware: >=1.0
- psr/simple-cache: >=1.0
- zendframework/zend-diactoros: >=2.1
Requires (Dev)
- mockery/mockery: >=1.2
- phpunit/phpunit: >=8.0
- squizlabs/php_codesniffer: >=3.0
- swoole/ide-helper: @dev
This package is auto-updated.
Last update: 2024-12-15 03:08:57 UTC
README
Requirements
>= PHP 7.2
swoole extension
zlib extension
Installation
composer install fatcode/http-server
Quick start
<?php declare(strict_types=1); use FatCode\HttpServer\HttpServer; use FatCode\HttpServer\Server\Router; use FatCode\HttpServer\Response; // Instantiates router for registering resources: $router = new Router(); $router->get('/hello', function () { return new Response('Hello You!'); }); // Run server at localhost:80 $server = new HttpServer(); $server->use($router); $server->start();
The above example creates server that uses router with registered one resource. Server will listen
on localhost
at port 8080
.
Please Note: Package is supporting PSR-7, that means all your registered handlers should expect
ServerRequestInterface
as an input, and returnResponseInterface
as a result.
Running server as a daemon
Http server provides flexible configuration class, depending on your settings server can be daemonized, run on specific port, listen to specific amount of incoming connections and so on.
More options can be found in the class docblock itself.
<?php declare(strict_types=1); use FatCode\HttpServer\HttpServer; use FatCode\HttpServer\Server\HttpServerSettings; // Setting pid file will make server run as a daemon. $settings = new HttpServerSettings('0.0.0.0', 8080); $settings->setPidFile(sys_get_temp_dir() . '/my_pid.pid'); // Note this server will always respond with 404 response, as there is // no router passed that can handle the request. $server = new HttpServer(); $server->start();
Middleware and PSR-15 support
Registering and using PSR-15 compatible middleware is trivial, just pass an instance of given middleware or closure itself
to HttpServer::use
method in the right order. In fact FatCode\HttpServer\Server\Router
class is PSR-15 middleware itself.
<?php declare(strict_types=1); use FatCode\HttpServer\HttpServer; use FatCode\HttpServer\Response; use Psr\Http\Message\ServerRequestInterface; // Simple pong server. $server = new HttpServer(); $server->use(function (ServerRequestInterface $request) : Response { if ($request->getUri()->getPath() === '/ping') { return new Response('Pong!'); } return new Response('Please call /ping uri.'); }); $server->start();
Request, Response and PSR-7
Http package provides convenient PSR-7 implementation based on zendframework/zend-diactoros
package.
Shortly saying FatCode\HttpServer\ServerRequest
and FatCode\HttpServer\Response
objects utilize zend-diactoros
exceptions and interface with some additions. When working with these objects please keep it in mind.
Creating new response object
<?php declare(strict_types=1); use FatCode\HttpServer\Response; use FatCode\HttpServer\HttpStatusCode; // Creates new response with status code 200. $response = new Response("Hello world!", HttpStatusCode::OK());
For your convenience package declares HttpStatusCode
enum that helps with generation valid http responses.
More status codes can be found in the class itself..
Working with request object
Reading route parameters
When declaring parametrized route you can access retrieve the value by calling FatCode\HttpServer\ServerRequest::getAttribute
method.
Consider the following example:
<?php declare(strict_types=1); use FatCode\HttpServer\HttpServer; use Psr\Http\Message\ServerRequestInterface; use FatCode\HttpServer\Response; use FatCode\HttpServer\Server\Router; // Initialize router $router = new Router(); // Register parametrized route $router->get('/hello/{name}', function (ServerRequestInterface $request) : Response { // Return response return new Response("Hello, your name is {$request->getAttribute('name')}"); }); // Setup http server $server = new HttpServer(); $server->use($router); $server->start();
For more examples you can visit examples directory.