Skip to content

Core\Router\Match::accessAs

$match->accessAs<T>(): ?T

Returns the access rule of the route that matched, as a case of the enum you write between < and >. For a route with #[Access(allow: App\Role::Admin)], accessAs<App\Role>() returns App\Role::Admin. A public route has the rule Core\Audience::Public, so you read it with accessAs<Core\Audience>().

If the rule is not a case of that enum, the result is null. Treat null as "access denied".

The server does not check this rule for you. With Core\Router::dispatch, the allows method of your gate checks it before each route method runs. A program that calls the route methods itself checks it once, before that call, so one check covers every route.

The compiler checks every comparison with the result. If you rename a case, a comparison with the old name does not compile. The type argument must be an enum. A rule that names a class constant has no enum, so read that rule with access().

The examples show the rule of each route, a check for one role, and one check that runs before every handler.

Return value

Returns ?T. The case of that enum, or null when the route's rule is not a case of that enum. Treat null as access denied. The type argument must be an enum.

Examples

The rule of each route

<?nvs
// Shows how to read the access rule of the route that matched as an enum case.
namespace App;
enum Role { Admin }
class Site
{
#[Core\Route(path: '/', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function home(): string
{
return 'home';
}
#[Core\Route(path: '/admin', method: Core\Http\Method::Get)]
#[Core\Access(allow: Role::Admin)]
public function admin(): string
{
return 'admin';
}
}
// `accessAs<Core\Audience>()` returns the case `Core\Audience::Public`.
// This prints `home is public`.
?Core\Router\Match $home = Core\Router::match(Core\Http\Method::Get, '/');
if ($home != null && $home->accessAs<Core\Audience>() == Core\Audience::Public) {
echo "home is public\n";
}
// The rule of `/admin` is a case of `Role`, not of `Core\Audience`.
// So `accessAs<Core\Audience>()` returns `null`, and this prints `admin is not public`.
?Core\Router\Match $admin = Core\Router::match(Core\Http\Method::Get, '/admin');
if ($admin != null && $admin->accessAs<Core\Audience>() == null) {
echo "admin is not public\n";
}
// `accessAs<Role>()` returns `Role::Admin`. This prints `admin needs Admin`.
if ($admin != null && $admin->accessAs<Role>() == Role::Admin) {
echo "admin needs Admin\n";
}
Output
home is public
admin is not public
admin needs Admin

A check for one role

<?nvs
// Shows how to allow a route only for users with one role.
namespace App;
enum Role { Editor }
class Articles
{
#[Core\Route(path: '/articles', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function list(): string
{
return 'all articles';
}
#[Core\Route(path: '/articles/edit', method: Core\Http\Method::Get)]
#[Core\Access(allow: Role::Editor)]
public function edit(): string
{
return 'the editor';
}
}
class Guard
{
// A public route is open to everyone. An editor route is open only to editors.
// Any other rule is denied.
public static function allows(Core\Router\Match $found, bool $isEditor): bool
{
if ($found->accessAs<Core\Audience>() == Core\Audience::Public) {
return true;
}
if ($found->accessAs<Role>() == Role::Editor) {
return $isEditor;
}
return false;
}
}
// A visitor who is not an editor. This prints `yes` and then `no`.
array<string> $paths = ['/articles', '/articles/edit'];
foreach ($paths as string $path) {
?Core\Router\Match $found = Core\Router::match(Core\Http\Method::Get, $path);
if ($found != null) {
echo $path, ': ', Guard::allows($found, false) ? 'yes' : 'no', "\n";
}
}
Output
/articles: yes
/articles/edit: no

One check before every handler

<?nvs
// Shows a program that calls its route methods itself and checks the access rule once, before any of them.
namespace App;
enum Role { Admin }
class Pages
{
#[Core\Route(path: '/', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function home(): string
{
return '200 Welcome';
}
#[Core\Route(path: '/admin/reports', method: Core\Http\Method::Get)]
#[Core\Access(allow: Role::Admin)]
public function reports(): string
{
return '200 Reports';
}
}
class FrontController
{
public static function answer(bool $isAdmin): string
{
?Core\Router\Match $matched = Core\Request::route();
if ($matched == null) {
return '404 Not Found';
}
// The server does not check the rule. This one check covers every route below.
bool $allowed = $matched->accessAs<Core\Audience>() == Core\Audience::Public
|| ($matched->accessAs<Role>() == Role::Admin && $isAdmin);
if (!$allowed) {
return '403 Forbidden';
}
var $pages = new Pages();
if ($matched->is(Pages::home)) {
return $pages->home();
} elseif ($matched->is(Pages::reports)) {
return $pages->reports();
}
return '404 Not Found';
}
}
// The request is `GET /admin/reports`. A real program reads the role from the session.
// The visitor is not an admin, so this prints `403 Forbidden`.
echo FrontController::answer(false), "\n";
Output
403 Forbidden