Skip to content

Core\Router\Match::method

$match->method(): Core\Http\Method

Returns the HTTP method of the route that matched, as a Core\Http\Method case. It is the method in the route's method:.

One handler can have two #[Route] attributes, for example one for Get and one for Post on the same path. is() returns true for both routes, because both belong to the same handler. method() tells them apart.

A HEAD request matches a Get route. For that request, the result is Core\Http\Method::Get.

The examples show the method of a match, a HEAD request, and one handler that shows a form for Get and saves it for Post.

Return value

Returns Core\Http\Method. The Core\Http\Method case written in the route's method:. A HEAD request matches a Get route, so the result is Core\Http\Method::Get.

Examples

The method of a match

<?nvs
// Shows how to read the HTTP method of the route that matched.
class Tasks
{
#[Core\Route(path: '/tasks', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function list(): string
{
return 'all tasks';
}
#[Core\Route(path: '/tasks', method: Core\Http\Method::Post)]
#[Core\Access(allow: Core\Audience::Public)]
public function create(): string
{
return 'a new task';
}
}
// Both routes have the path `/tasks`. A `Post` matches the second route.
?Core\Router\Match $found = Core\Router::match(Core\Http\Method::Post, '/tasks');
if ($found != null) {
// `method()` returns a `Core\Http\Method` case. This prints `Post`.
echo $found->method() == Core\Http\Method::Post ? 'Post' : 'not Post', "\n";
// The route belongs to the method `create`. This prints `Tasks::create`.
echo $found->is(Tasks::create) ? 'Tasks::create' : 'another route', "\n";
}
Output
Post
Tasks::create

A head request

<?nvs
// Shows that a `HEAD` request matches a `Get` route, and `method()` returns `Get`.
class Reports
{
#[Core\Route(path: '/reports/daily', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function daily(): string
{
return 'the daily report';
}
}
// A `HEAD` request asks for the headers of a page and not its body.
?Core\Router\Match $found = Core\Router::match(Core\Http\Method::Head, '/reports/daily');
if ($found != null) {
// The route is a `Get` route, so this prints `Get`.
echo $found->method() == Core\Http\Method::Get ? 'Get' : 'not Get', "\n";
}
Output
Get

One handler for a form

<?nvs
// Shows one handler that shows a form for `Get` and saves the form for `Post`.
class Newsletter
{
// The two routes have the same path, and both belong to `signup`.
#[Core\Route(path: '/newsletter', method: Core\Http\Method::Get)]
#[Core\Route(path: '/newsletter', method: Core\Http\Method::Post)]
#[Core\Access(allow: Core\Audience::Public)]
public function signup(): string
{
// `method()` tells the two routes apart. `?->` gives `null` when there is no match.
?Core\Router\Match $matched = Core\Request::route();
if ($matched?->method() == Core\Http\Method::Post) {
return 'Thank you. You are now subscribed.';
}
return 'Enter your email address to subscribe.';
}
}
// The request is `POST /newsletter`, so this prints the thank-you text.
var $newsletter = new Newsletter();
echo $newsletter->signup(), "\n";
Output
Thank you. You are now subscribed.