Core\Audience
Names the one audience Novis itself understands: everyone.
Every route in a Novis program says who may reach it, and it says so by naming a value rather than by writing a piece of text. Core\Audience::Public is the name for anyone may call this route, and it is the only case this enum has. It will never gain a second one. Every other answer — a signed-in customer, an administrator, somebody on one team — belongs to your application, so you write your own enum or class constant for it and give that to the route instead. Novis checks that the name you wrote exists. It never decides what the name means.
Good to know: a plain string or number is refused while your program is compiled, so a misspelt "public" can never quietly open a route.
Examples
A route anyone may call
<?nvs// Every route in a Novis program says who may reach it. These two are open to// anyone, and `Core\Audience::Public` is the name for that.
class Pages{ #[Core\Route(path: '/pricing', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function pricing(): string { return 'our prices'; }
#[Core\Route(path: '/help/{topic}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function help(string $topic): string { return $topic; }}
// Links to those two pages. This prints `/pricing` and then `/help/billing`.echo Core\Router::url(Pages::pricing), "\n";echo Core\Router::url(Pages::help, ['topic' => 'billing']), "\n";
// The case is an ordinary value as well. You can keep it in a variable and// compare it, which is what code of your own that sorts routes by audience does.var $audience = Core\Audience::Public;echo $audience == Core\Audience::Public ? 'open to everyone' : 'not open', "\n";/pricing/help/billingopen to everyoneYour own names for everyone else
<?nvs// Novis names one audience only: everyone. Every other answer belongs to your// program, so this one writes its own names for the routes that are not open.
class Plan{ public const string PAID = 'paid';}
class Site{ #[Core\Route(path: '/', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function home(): string { return 'home'; }
// A case of your own enum. Novis checks that the name exists, nothing more. #[Core\Route(path: '/account', method: Core\Http\Method::Get)] #[Core\Access(allow: Gate::Role::Member)] public function account(): string { return 'account'; }
// A class constant is a name too, so it works here just as well. #[Core\Route(path: '/reports', method: Core\Http\Method::Get)] #[Core\Access(allow: Plan::PAID)] public function reports(): string { return 'reports'; }}
// What your own names mean is your program's decision, and this is where it// gets made. Novis never makes it for you.class Gate{ enum Role { Admin, Member }
public static function sees(Role $role): string { return match ($role) { Role::Admin => 'every account', Role::Member => 'their own account', }; }}
echo Core\Router::url(Site::home), "\n";echo Core\Router::url(Site::account), "\n";echo 'admin sees ', Gate::sees(Gate::Role::Admin), "\n";echo 'member sees ', Gate::sees(Gate::Role::Member), "\n";//accountadmin sees every accountmember sees their own accountA webhook another server calls
<?nvs// A payment provider calls your server when a payment goes through. It has no// account with you, so its route is open to everyone. It also sends no browser// form, so the form token check has nothing to check and is turned off here.
class Checkout{ // A form a shopper submits. Open to everyone, and the token check stays on. #[Core\Route(path: '/checkout', method: Core\Http\Method::Post)] #[Core\Access(allow: Core\Audience::Public)] public function start(): string { return 'started'; }
// The provider's call. Open as well, and without the token check. #[Core\Route(path: '/hooks/payments', method: Core\Http\Method::Post)] #[Core\Access(allow: Core\Audience::Public, csrf: false)] public function settled(): string { return 'recorded'; }}
// `Core\Audience::Public` means everyone may call the route. The webhook must// still check the signature the provider sends, because it does not use a session.echo Core\Router::url(Checkout::start), "\n";echo Core\Router::url(Checkout::settled), "\n";/checkout/hooks/payments