yiisoft/http
Lightweight HTTP utilities for PHP from the Yii ecosystem, offering constants for methods, status codes, headers, and MIME types plus helpers for PSR-7 messages. Useful for building consistent HTTP layers without a full framework.
yiisoft/http is a constants-only utility package: no service provider, no config, no runtime behavior. It gives you Yiisoft\Http\Method, Yiisoft\Http\StatusCode, and (depending on version) Yiisoft\Http\Header classes full of public const values.
composer require yiisoft/http
Where to look first:
vendor/yiisoft/http/src/Method.php — HTTP verb constants (Method::GET, Method::POST, …).vendor/yiisoft/http/src/StatusCode.php — status code constants (StatusCode::OK, StatusCode::NOT_FOUND, …).composer.json — confirm the PHP version floor and whether Header is included in your installed tag.First use case: replace magic strings and integers in HTTP-facing code. For example, a middleware that only accepts POST:
use Yiisoft\Http\Method;
use Yiisoft\Http\StatusCode;
public function handle(Request $request, Closure $next)
{
if (! $request->isMethod(Method::POST)) {
return response()->json(['error' => 'Method Not Allowed'], StatusCode::METHOD_NOT_ALLOWED);
}
return $next($request);
}
That is the honest scope of the package. It will not change how Laravel routes, validates, or responds — it only makes the literals self-documenting and greppable.
Controller responses. Use status constants where you already return integer codes, especially for non-200 responses:
return response()->json($resource, StatusCode::CREATED);
return response()->noContent(StatusCode::NO_CONTENT);
Middleware and guards. Method constants read better than strings and pair naturally with Laravel's isMethod():
if ($request->isMethod(Method::PUT) || $request->isMethod(Method::PATCH)) {
// ...
}
Route definitions. Laravel's router accepts string verbs, so Route::match([Method::GET, Method::HEAD], ...) works, but it is marginal value because the router already validates verbs. Use it only if your team wants one vocabulary across routes, middleware, and tests.
Tests. Constants make assertions less ambiguous:
$response->assertStatus(StatusCode::UNPROCESSABLE_ENTITY);
This is arguably the highest-value place to adopt the package — test suites accumulate a lot of bare 422, 404, 201.
Custom exception handler / API error mapping. Centralize a mapping from domain exceptions to status constants so the handler reads declaratively:
protected $statusMap = [
ModelNotFoundException::class => StatusCode::NOT_FOUND,
ValidationException::class => StatusCode::UNPROCESSABLE_ENTITY,
];
CORS and header middleware. If your installed version ships Header, use its constants when building Access-Control-Allow-Methods / Allow values or comparing incoming headers. Verify the class exists before depending on it; do not assume parity with every header name you need.
Gradual adoption. There is no migration cost, so introduce it file-by-file. A practical rule: adopt it in new middleware, API controllers, and tests; leave existing form requests and Blade alone rather than churning diffs.
Static analysis. The real payoff in a large Laravel codebase is PHPStan/Psalm catching a typo like StatusCode::NOT_FOUNDD at analysis time instead of at runtime. If you already run static analysis in CI, this package becomes more attractive.
Symfony\Component\HttpFoundation\Response::HTTP_OK and Request::METHOD_GET. Do not mix both vocabularies in one codebase — pick one and enforce it with a code-review note or a PHP-CS-Fixer rule.StatusCode::NOT_FOUND vs Response::HTTP_NOT_FOUND; Method::GET vs Request::METHOD_GET. Copy-pasting between the two will produce undefined-constant errors.StatusCode::isSuccess() or similar predicates. If you want isSuccessful($code), write a small helper or use Symfony's Response::isSuccessful().Yiisoft\Http\Header may not exist, or may not cover every header you need, depending on the tagged version. Check src/ before writing code against it, and be prepared to fall back to literals.$request->header() lookups.composer show yiisoft/http to confirm the installed version, then grep -n "const YOUR_NAME" vendor/yiisoft/http/src/*.php. There is nothing else to debug.StatusCode::TEAPOT is there, but custom 4xx business codes are not), define your own App\Http\Status class and optionally reference the package constants inside it.How can I help you explore Laravel packages today?