Core\Router::methodsFor
Core\Router::methodsFor(string $path): array<Core\Http\Method>Core\Router::methodsFor( string $path, ): array<Core\Http\Method>
Lists the HTTP methods that the routes of a path accept. Core\Router::methodsFor checks the path against every #[Route] in your program and returns an array of Core\Http\Method values. Each method is in the array once, in the order the routes are declared.
Use it when a request matches no route, to choose the status of the response. An empty array means that no route has this path, so the status is 404 Not Found. A non-empty array means that the path exists for other methods, so the status is 405 Method Not Allowed. The array is then the list for its Allow header. Core\Router::dispatch makes this choice for you. You need methodsFor when your program calls the route methods itself.
A capture that does not fit its handler parameter does not match. For example, abc in the place of a uint $id gives an empty array. The method never throws an error.
The examples list the methods of one path, show a path that no route has, and choose between a 404 and a 405.
Parameters
$pathstringThe path, with no query string. The path of a request already has its mount prefix removed.
Return value
Returns array<Core\Http\Method>. The methods, each one once. An empty array means that no route has this path, so the status is 404. Otherwise the status is 405, and the array is the list for the Allow header. A capture is a value from the path. Each capture must be a valid value of its handler parameter's type, such as int, Core\Uuid or Core\Time\Date. Otherwise the path does not match. It also does not match when the capture is not one of the values its parameter allows.
Examples
The methods of one path
<?nvs// Shows how to list the HTTP methods that the routes of one path accept.
class Comments{ #[Core\Route(path: '/comments', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function list(): string { return 'all comments'; }
#[Core\Route(path: '/comments', method: Core\Http\Method::Post)] #[Core\Access(allow: Core\Audience::Public)] public function add(): string { return 'a new comment'; }}
// `Core\Router::methodsFor` returns the methods in the order the routes are declared.// `Core\Http::methodName` converts each one to text. This prints `GET` and `POST`.foreach (Core\Router::methodsFor('/comments') as Core\Http\Method $verb) { echo Core\Http::methodName($verb), "\n";}GETPOSTA path no route has
<?nvs// Shows what `Core\Router::methodsFor` returns for a path that no route has.
class Users{ #[Core\Route(path: '/users/{id}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $id): string { return 'one user'; }}
// `/users/7` matches the route, so this prints 1.echo Core\Arr::count(Core\Router::methodsFor('/users/7')), "\n";
// `{id}` is a `uint` in `show`, and `abc` is not a number.// No route has this path, so the result is an empty array and this prints 0.echo Core\Arr::count(Core\Router::methodsFor('/users/abc')), "\n";10Choose between 404 and 405
<?nvs// Shows how to choose between a `404` and a `405` status when a request matches no route.
class Invoices{ #[Core\Route(path: '/invoices', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function list(): string { return 'all invoices'; }
#[Core\Route(path: '/invoices', method: Core\Http\Method::Post)] #[Core\Access(allow: Core\Audience::Public)] public function create(): string { return 'a new invoice'; }}
class NoRoute{ // A server calls this when `Core\Request::route()` returns `null`. public static function describe(string $path): string { array<Core\Http\Method> $verbs = Core\Router::methodsFor($path); // No methods means that the path does not exist, so the status is `404`. if (Core\Arr::count($verbs) == 0) { return '404 Not Found'; } // The path exists for other methods, so the status is `405`. // The `Allow` header lists those methods. string $allow = ''; foreach ($verbs as Core\Http\Method $verb) { if ($allow != '') { $allow = $allow . ', '; } // `Core\Http::methodName` converts the method to text, for example `GET`. $allow = $allow . Core\Http::methodName($verb); } return '405 Method Not Allowed, Allow: ' . $allow; }}
// A `Delete` to `/invoices` matches no route. A `Get` to `/bills` also matches no route.// These two lines print the status the server sends for each one.echo NoRoute::describe('/invoices'), "\n";echo NoRoute::describe('/bills'), "\n";405 Method Not Allowed, Allow: GET, POST404 Not Found