Core\Http\Method
The HTTP methods a route can be declared with, and a request can arrive with. There are exactly eight of them.
Get, Head, Options and Trace only read something. Post, Put, Patch and Delete may change it, and the cross-site request check covers those four. You use a case when you declare a route. The router also returns cases when you check which methods a path has routes for.
The list of cases is fixed, so a method is a value the compiler knows. You do not write it as text. A misspelled method does not compile, so it cannot become a route that nobody can reach.
Good to know: there is no CONNECT case. A CONNECT request tells a proxy to open a tunnel, and a route in your program never handles that.
Examples
The eight verbs and what each is for
<?nvs// Every verb you can name, what each one is for, and whether it is allowed to// change anything. The four that can change something are the ones a// cross-site request check has to cover.
class Verbs{ public static function purpose(Core\Http\Method $verb): string { return match ($verb) { Core\Http\Method::Get => 'Get: reads a page, and changes nothing', Core\Http\Method::Head => 'Head: the same as Get, but the answer has no body', Core\Http\Method::Options => 'Options: asks what a page supports', Core\Http\Method::Trace => 'Trace: sends the request back, for working out what a proxy did to it', Core\Http\Method::Post => 'Post: sends something new, such as a filled-in form', Core\Http\Method::Put => 'Put: replaces what is there with what you send', Core\Http\Method::Patch => 'Patch: changes part of what is there', Core\Http\Method::Delete => 'Delete: removes it', }; }
public static function changesThings(Core\Http\Method $verb): bool { return match ($verb) { Core\Http\Method::Get => false, Core\Http\Method::Head => false, Core\Http\Method::Options => false, Core\Http\Method::Trace => false, Core\Http\Method::Post => true, Core\Http\Method::Put => true, Core\Http\Method::Patch => true, Core\Http\Method::Delete => true, }; }}
array<Core\Http\Method> $everything = [ Core\Http\Method::Get, Core\Http\Method::Head, Core\Http\Method::Options, Core\Http\Method::Trace, Core\Http\Method::Post, Core\Http\Method::Put, Core\Http\Method::Patch, Core\Http\Method::Delete,];
foreach ($everything as Core\Http\Method $verb) { echo Verbs::changesThings($verb) ? 'changes ' : 'reads ', Verbs::purpose($verb), "\n";}reads Get: reads a page, and changes nothingreads Head: the same as Get, but the answer has no bodyreads Options: asks what a page supportsreads Trace: sends the request back, for working out what a proxy did to itchanges Post: sends something new, such as a filled-in formchanges Put: replaces what is there with what you sendchanges Patch: changes part of what is therechanges Delete: removes itDeclaring a route under a verb
<?nvs// Each route has one HTTP verb. The same path with another verb is a different route.// `Core\Router::match` takes a verb and a path and returns the route that would be called.
class Pages{ // Every route also says who may reach it. `Public` means anybody. #[Core\Route(path: '/pages/{slug}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(string $slug): string { return $slug; }
#[Core\Route(path: '/pages', method: Core\Http\Method::Post)] #[Core\Access(allow: Core\Audience::Public)] public function create(): string { return 'made'; }}
// Reading a page. `match` does not call the route. It returns the route, and you// decide what to do with it. `is` checks which method the route belongs to.?Core\Router\Match $read = Core\Router::match(Core\Http\Method::Get, '/pages/intro');if ($read != null && $read->is(Pages::show)) { echo "get /pages/intro -> Pages::show\n";}
// Creating one.?Core\Router\Match $made = Core\Router::match(Core\Http\Method::Post, '/pages');if ($made != null && $made->is(Pages::create)) { echo "post /pages -> Pages::create\n";}
// The right path with the wrong verb matches nothing. No route is declared for// `Post` at `/pages/intro`, so the result is `null`.?Core\Router\Match $wrong = Core\Router::match(Core\Http\Method::Post, '/pages/intro');if ($wrong == null) { echo "post /pages/intro -> no route\n";}get /pages/intro -> Pages::showpost /pages -> Pages::createpost /pages/intro -> no routeAnswering a wrong verb with the right list
<?nvs// A visitor sends the wrong verb to a page that does exist. The polite answer// says so and lists the verbs that would have worked, which is a different// answer from "there is no such page".
class Orders{ #[Core\Route(path: '/orders', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function list(): string { return 'list'; }
#[Core\Route(path: '/orders', method: Core\Http\Method::Post)] #[Core\Access(allow: Core\Audience::Public)] public function create(): string { return 'created'; }}
class Verbs{ // Says what to answer for one verb and one path. public static function answer(Core\Http\Method $verb, string $path): string { if (Core\Router::match($verb, $path) != null) { return '200, the route runs'; }
array<Core\Http\Method> $allowed = Core\Router::methodsFor($path); if (Core\Arr::count($allowed) == 0) { return '404, no such page'; }
string $list = ''; foreach ($allowed as Core\Http\Method $other) { // `Core\Http::methodName` converts the method to text, for example `GET`. string $name = Core\Http::methodName($other); $list = $list == '' ? $name : $list . ', ' . $name; } return '405, try one of: ' . $list; }}
echo 'GET /orders -> ', Verbs::answer(Core\Http\Method::Get, '/orders'), "\n";echo 'DELETE /orders -> ', Verbs::answer(Core\Http\Method::Delete, '/orders'), "\n";echo 'GET /invoices -> ', Verbs::answer(Core\Http\Method::Get, '/invoices'), "\n";GET /orders -> 200, the route runsDELETE /orders -> 405, try one of: GET, POSTGET /invoices -> 404, no such page