Skip to content

An HTTP server plugin to serve static files like HTML, CSS, JavaScript, and images effortlessly.

License

Notifications You must be signed in to change notification settings

amphp/http-server-static-content

Folders and files

NameName
Last commit message
Last commit date
Nov 12, 2022
Aug 15, 2023
Apr 15, 2018
Jan 23, 2024
Apr 22, 2023
Mar 12, 2018
Oct 26, 2020
Apr 9, 2022
Aug 15, 2023
Nov 27, 2023
Apr 9, 2022
Aug 15, 2023
Sep 16, 2019
Feb 5, 2023

Repository files navigation

http-server-static-content

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:

<?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;
// $server is an instance of HttpServer and $errorHandler an instance of ErrorHandler
$router->setFallback(new DocumentRoot($server, $errorHandler, __DIR__ . '/public'));
$router->addRoute('GET', '/', new ClosureRequestHandler(function () {
    return new Response(Status::OK, ['content-type' => 'text/plain'], 'Hello, world!');
}));


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

A full example is found in examples/server.php.

Contributing

Please read our rules for details on our code of conduct, and the process for submitting pull requests to us.

Security

If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.