Skip to content

HTTP request

composer require quillstack/http-request

Common interface for Server Requests and Requests, based on PSR-7.

Every HTTP method, in one place. Packages which need to name a method — a router registering a route, a request being built from globals, a middleware answering a preflight — take it from here rather than each writing its own string, so a typo is a missing constant rather than a route which never matches.

Requirements

  • PHP 8.1 or newer

Installation

shell
composer require quillstack/http-request

Usage

php
use Quillstack\HttpRequest\HttpRequest;

$router->map(HttpRequest::METHOD_GET, '/users', UserController::class);

if (!in_array($method, HttpRequest::AVAILABLE_METHODS, true)) {
    throw new BadRequestHttpException("Unknown method: {$method}");
}

Technical documentation

HttpRequest holds one constant per method and one list of all of them.

ConstantValue
HttpRequest::METHOD_GETGET
HttpRequest::METHOD_POSTPOST
HttpRequest::METHOD_HEADHEAD
HttpRequest::METHOD_PUTPUT
HttpRequest::METHOD_DELETEDELETE
HttpRequest::METHOD_PATCHPATCH
HttpRequest::METHOD_OPTIONSOPTIONS

HttpRequest::AVAILABLE_METHODS is all seven of them, in the order above. Walking it is how quillstack/router works out which methods a path answers to when it has to send 405 Method Not Allowed with an Allow header.

There is nothing else here on purpose: a package which only names things has no behaviour to get wrong, and everything depending on it stays free of everything else.

Unit tests

shell
composer test

Docker

shell
docker-compose up -d
docker exec -w /var/www/html -it quillstack_http-request sh

License

MIT. See LICENSE.

Released under the MIT License.