Click the Castor logo or press Ctrl Alt T to change theme.
# Methods `Castor\Net\Http\Method` is a backed enum for standard HTTP request methods. `Request` stores the method as this enum, which gives you type safety and convenient semantic helpers. ## Available methods The enum contains these cases: | Case | Value | | --- | --- | | `Method::Get` | `GET` | | `Method::Post` | `POST` | | `Method::Put` | `PUT` | | `Method::Patch` | `PATCH` | | `Method::Delete` | `DELETE` | | `Method::Options` | `OPTIONS` | | `Method::Head` | `HEAD` | | `Method::Trace` | `TRACE` | | `Method::Connect` | `CONNECT` | ## Creating methods Use normal enum APIs or let `Request::create` and `Request::copyWith` coerce method strings for you: ```php <?php use Castor\Net\Http\Method; use Castor\Net\Http\Request; $method = Method::from('GET'); $request = Request::create('POST', 'https://api.example.com/users'); $put = $request->copyWith(method: 'PUT'); $delete = $request->copyWith(method: Method::Delete); ``` Because this is a backed enum, invalid strings throw `ValueError` when passed to `Method::from` or to request factories that call it. ## Safe methods A safe method is intended for retrieval and should not change server state as a result of the request semantics. ```php Method::Get->isSafe(); // true Method::Head->isSafe(); // true Method::Options->isSafe(); // true Method::Trace->isSafe(); // true Method::Post->isSafe(); // false ``` Castor HTTP marks `GET`, `HEAD`, `TRACE`, and `OPTIONS` as safe. All other enum cases return `false`. ## Idempotent methods An idempotent method can be repeated without changing the intended effect beyond the first request. ```php Method::Get->isIdempotent(); // true Method::Put->isIdempotent(); // true Method::Delete->isIdempotent(); // true Method::Post->isIdempotent(); // false Method::Patch->isIdempotent(); // false ``` The helper returns `false` for `POST` and `PATCH`, and `true` for every other method case. ## Common use in handlers ```php use Castor\Net\Http\Status\Code; if ($request->method !== Method::Post) { $writer->writeHeaders(Code::MethodNotAllowed); return; } ``` Use the enum for exact routing decisions, and use `isSafe` or `isIdempotent` for cross-cutting behavior like retries, caching policy, and request logging.
Castor ecosystem