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
composer require quillstack/http-requestUsage
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.
| Constant | Value |
|---|---|
HttpRequest::METHOD_GET | GET |
HttpRequest::METHOD_POST | POST |
HttpRequest::METHOD_HEAD | HEAD |
HttpRequest::METHOD_PUT | PUT |
HttpRequest::METHOD_DELETE | DELETE |
HttpRequest::METHOD_PATCH | PATCH |
HttpRequest::METHOD_OPTIONS | OPTIONS |
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
composer testDocker
docker-compose up -d
docker exec -w /var/www/html -it quillstack_http-request shLicense
MIT. See LICENSE.