Click the Castor logo or press Ctrl Alt T to change theme.
# Status codes Castor HTTP separates status handling into two types: - `Castor\Net\Http\Status\Code` is a backed enum of known HTTP status codes. - `Castor\Net\Http\Status` is the status line value stored on a response: an integer code plus a reason phrase. This lets you use enum cases for known codes while still representing non-standard codes when you need to. ## Creating a status Use `Status::fromCode` when you have a `Code` enum case: ```php <?php use Castor\Net\Http\Status; use Castor\Net\Http\Status\Code; $status = Status::fromCode(Code::Created); $status->code; // 201 $status->phrase; // "Created" ``` Use `Status::coerce` when you accept either an enum case or an integer: ```php Status::coerce(Code::OK); // 200 "OK" Status::coerce(404); // 404 "Not Found" Status::coerce(599); // 599 "Undefined Phrase" ``` If an integer is not represented by the enum, Castor keeps the integer and uses `Undefined Phrase` as the reason phrase. ## Creating responses with status codes `Response::create` uses `Status::coerce` internally: ```php <?php use Castor\Net\Http\Response; use Castor\Net\Http\Status\Code; $ok = Response::create(Code::OK); $created = Response::create(201); ``` `ResponseWriter::writeHeaders` also accepts either a `Code` enum case or an integer: ```php $writer->writeHeaders(Code::NoContent); $writer->writeHeaders(422); ``` ## Range helpers `Status` provides helpers for common HTTP classes: ```php $status->isSuccess(); // true for 200..299 $status->isRedirect(); // true for 300..399 $status->isClientError(); // true for 400..499 $status->isServerError(); // true for 500..599 $status->inRange(100, 199); // custom range check ``` These helpers are used by `ProtocolError::check` to decide whether a response should be treated as an unexpected protocol result. ## Known status codes The `Code` enum includes common informational, successful, redirection, client error, and server error statuses: | Range | Examples | | --- | --- | | 1xx | `Continue`, `SwitchingProtocols`, `Processing`, `EarlyHints` | | 2xx | `OK`, `Created`, `Accepted`, `NoContent`, `PartialContent`, `MultiStatus`, `ImUsed` | | 3xx | `MultipleChoices`, `MovedPermanently`, `Found`, `SeeOther`, `NotModified`, `TemporaryRedirect`, `PermanentRedirect` | | 4xx | `BadRequest`, `Unauthorized`, `Forbidden`, `NotFound`, `MethodNotAllowed`, `Conflict`, `UnprocessableEntity`, `TooManyRequests`, `UnavailableForLegalReasons` | | 5xx | `InternalServerError`, `NotImplemented`, `BadGateway`, `ServiceUnavailable`, `GatewayTimeout`, `HTTPVersionNotSupported`, `NetworkAuthenticationRequired` | Each enum case exposes `reasonPhrase()`: ```php Code::ImATeapot->value; // 418 Code::ImATeapot->reasonPhrase(); // "I'm a Teapot" ``` ## Custom status lines For uncommon protocols or application-specific transports, instantiate `Status` directly: ```php $status = new Status(299, 'Custom Success'); ``` Direct construction is useful when the code is not part of the enum but you still need a meaningful phrase in serialized output.
Castor ecosystem