amphp/http-server-static-content

Static content request handler for Amp's HTTP server.

Fund package maintenance!
amphp

v2.0.0-beta.3 2023-02-05 20:54 UTC

This package is auto-updated.

Last update: 2023-05-23 03:21:45 UTC


README

This package provides a static content RequestHandler implementations for the AMPHP HTTP server.

Usage

DocumentRoot and StaticResource implement RequestHandler.

Example

<?php

use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Amp\Http\Server\StaticContent\DocumentRoot;
use Amp\Http\Status;

$router = new Amp\Http\Server\Router;
$router->setFallback(new DocumentRoot(__DIR__ . '/public'));
$router->addRoute('GET', '/', new ClosureRequestHandler(function () {
    return new Response(Status::OK, ['content-type' => 'text/plain'], 'Hello, world!');
}));


$server->start($router, new DefaultErrorHandler());