You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides an HTTP server plugin to serve static files like HTML, CSS, JavaScript, and images effortlessly.
Installation
This package can be installed as a Composer dependency.
composer require amphp/http-server-static-content
Usage
This package provides two RequestHandler implementations:
DocumentRoot: Serves all files within a directory.
StaticResource: Serves a single specific file.
The example below combines static file serving and request routing to demonstrate how they work well together:
<?phpuseAmp\Http\Server\DefaultErrorHandler;
useAmp\Http\Server\RequestHandler\ClosureRequestHandler;
useAmp\Http\Server\Response;
useAmp\Http\Server\SocketHttpServer;
useAmp\Http\Server\StaticContent\DocumentRoot;
useAmp\Http\Status;
$router = newAmp\Http\Server\Router;
// $server is an instance of HttpServer and $errorHandler an instance of ErrorHandler$router->setFallback(newDocumentRoot($server, $errorHandler, __DIR__ . '/public'));
$router->addRoute('GET', '/', newClosureRequestHandler(function () {
returnnewResponse(Status::OK, ['content-type' => 'text/plain'], 'Hello, world!');
}));
$server->start($router, newDefaultErrorHandler());