Core\Router\Match::attribute
$match->attribute<T>(): ?TReturns an attribute of the method of the route that matched. You write the name of an attribute declaration between < and >. For attribute Cache on method { seconds: int } and a method with #[Cache(seconds: 60)], $route->attribute<Cache>() returns { seconds: 60 }. If the method has no Cache attribute, the result is null.
Each kind of attribute has its own declaration. So one method can have a cache setting and a rate limit, and you read each one by its name. A gate uses this to read a setting for one route.
The type argument must be the name of an attribute declaration. If one route method has two attributes of that kind, the program does not compile. The compiler checks every route method, because any route can match.
Each call builds one object for each route method that has an attribute of that kind. A program with more such routes does more work per call.
The examples show a setting for each route, two settings on one method, and a gate that reads a cache setting.
Return value
Returns ?T. The attribute as an object of that shape, or null when the method has no attribute with every field of the shape. The type argument must be a shape. Two matching attributes on one method do not compile.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Attributes::get<Table> returns only the attributes written as #[Table(...)], so two attributes with the same fields are not mixed up. For a method that a class inherits and does not override, get and all return the attributes its parent class wrote on it. Core\Reflect\AttributeInfo::name returns the full name of the declaration. |
Examples
A setting for each route
<?nvs// Shows how to read an attribute of the method of the route that matched.
attribute Cache on method { seconds: int }
class Pages{ #[Core\Route(path: '/news', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] #[Cache(seconds: 60)] public function news(): string { return 'news'; }
#[Core\Route(path: '/live', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function live(): string { return 'live'; }}
// The method of `/news` has `#[Cache(seconds: 60)]`. This prints `news: 60 seconds`.?Core\Router\Match $news = Core\Router::match(Core\Http\Method::Get, '/news');if ($news != null) { var $cache = $news->attribute<Cache>(); if ($cache != null) { echo 'news: ', $cache->seconds, " seconds\n"; }}
// The method of `/live` has no `Cache` attribute, so the result is `null`.// This prints `live: no setting`.?Core\Router\Match $live = Core\Router::match(Core\Http\Method::Get, '/live');if ($live != null && $live->attribute<Cache>() == null) { echo "live: no setting\n";}news: 60 secondslive: no settingTwo settings on one method
<?nvs// Shows how one route method can have two settings, and how to read each one by its name.
attribute Cache on method { seconds: int, shared: bool }attribute RateLimit on method { perMinute: int }
class Search{ #[Core\Route(path: '/search', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] #[Cache(seconds: 30, shared: true)] #[RateLimit(perMinute: 20)] public function results(): string { return 'results'; }}
?Core\Router\Match $route = Core\Router::match(Core\Http\Method::Get, '/search');if ($route != null) { // `attribute<Cache>` returns only the `Cache` attribute. This prints `cache: 30 seconds`. var $cache = $route->attribute<Cache>(); if ($cache != null) { echo 'cache: ', $cache->seconds, " seconds\n"; }
// `attribute<RateLimit>` returns only the `RateLimit` attribute. // This prints `limit: 20 per minute`. var $limit = $route->attribute<RateLimit>(); if ($limit != null) { echo 'limit: ', $limit->perMinute, " per minute\n"; }}cache: 30 secondslimit: 20 per minuteA gate that sets a cache header
<?nvs// Shows a gate that reads the cache setting of each route and sends a `Cache-Control` header.
use Core\Http\Method;use Core\Router\Refusal;
attribute Cache on method { seconds: int }
class Pages{ #[Core\Route(path: '/news', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] #[Cache(seconds: 60)] public function news(): void { echo 'news'; }
#[Core\Route(path: '/account', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function account(): void { echo 'account'; }}
class Gate implements Core\Router\Gate{ public function allows(Core\Router\Match $route): bool { // A route with a cache setting may be cached for that many seconds. // A route without one is never cached. var $cache = $route->attribute<Cache>(); if ($cache != null) { Core\Response::setHeader('Cache-Control', 'max-age=' . $cache->seconds); } else { Core\Response::setHeader('Cache-Control', 'no-store'); } return true; }
public function refused(Refusal $reason, ?Core\Router\Match $route): void { echo 'no page here'; }
public function failed(?Core\Router\Match $route, ?Throwable $error): void { echo 'something went wrong'; }}
if (Core\Env::context() == Core\Env::Context::Request) { Core\Router::dispatch(new Gate());} else { // From the command line, this part sends two requests. It needs no server. // This prints `news max-age=60`, then `account no-store`. var $news = Core\Test::request(Method::Get, '/news'); echo $news->body(), ' ', $news->header('Cache-Control') ?? '', "\n"; var $account = Core\Test::request(Method::Get, '/account'); echo $account->body(), ' ', $account->header('Cache-Control') ?? '', "\n";}news max-age=60account no-store