Core\Router\Match::access
$match->access(): ?stringReturns the access rule of the route that matched, as text. It is the full name of the constant in the route's #[Access(allow: …)], such as Core\Audience::Public or App\Role::Admin. The result is text from your program and not from the request, so it is not tainted.
To check access, use accessAs<E>(). It returns the rule as a case of the enum E, and the compiler checks the comparison. A comparison with the text still compiles after a case is renamed, and then it never matches again. Use access() to show or log the rule, and for a rule that names a class constant, which has no enum.
Every route in a program has an #[Access], so the result is normally never null. If it is null, treat it as "access denied".
The examples show the rule of each route, a rule that names a class constant, and the rule of the current request in a log line.
Return value
Returns ?string. The name with its namespace, such as Core\Audience::Public or App\Role::Admin. Not tainted: it is the program's own text. Every route of a compiled program has one, so treat null as access denied.
Examples
The rule of each route
<?nvs// Shows how to read the access rule of the route that matched.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'; }}
// `access()` returns the full name of the constant in `#[Access(allow: …)]`.// This prints `Core\Audience::Public`.?Core\Router\Match $home = Core\Router::match(Core\Http\Method::Get, '/');if ($home != null) { echo $home->access() ?? 'denied', "\n";}
// The name includes the namespace. This prints `App\Role::Admin`.?Core\Router\Match $admin = Core\Router::match(Core\Http\Method::Get, '/admin');if ($admin != null) { echo $admin->access() ?? 'denied', "\n";}Core\Audience::PublicApp\Role::AdminA rule that names a class constant
<?nvs// Shows how to read an access rule that names a class constant.namespace App;
class Policy{ public const string STAFF = 'staff';}
class Office{ #[Core\Route(path: '/office', method: Core\Http\Method::Get)] #[Core\Access(allow: Policy::STAFF)] public function home(): string { return 'the office'; }}
// A class constant is not an enum case, so only `access()` returns this rule.// The result is the name of the constant. This prints `App\Policy::STAFF`.?Core\Router\Match $found = Core\Router::match(Core\Http\Method::Get, '/office');if ($found != null) { echo $found->access() ?? 'denied', "\n";}App\Policy::STAFFThe rule in a log line
<?nvs// Shows how to write the access rule of the current request into a log line.namespace App;
enum Role { Admin }
class Reports{ #[Core\Route(path: '/admin/reports', method: Core\Http\Method::Get)] #[Core\Access(allow: Role::Admin)] public function list(): string { return 'reports'; }}
// The request is `GET /admin/reports`. `Core\Request::route()` returns its match.// This prints `Reports::list needs App\Role::Admin`.?Core\Router\Match $matched = Core\Request::route();if ($matched != null) { string $page = $matched->is(Reports::list) ? 'Reports::list' : 'another page'; echo $page, ' needs ', $matched->access() ?? 'nothing', "\n";}Reports::list needs App\Role::Admin