PublicRouteRegistrar
in package
Public (front-end) routing surface: maps pretty URLs to handlers via the WordPress rewrite system, the third routing surface beside the REST {@see RestRouteRegistrar} and the admin {@see AdminRouteRegistrar}.
All routes for one component funnel through a single per-component query var
({component}_route, derived from componentName() — no brand literal), so two
plugins never collide on a shared query_vars slot. On register() it adds one
rewrite rule per route, registers the query var, and hooks template_redirect
to dispatch. Rewrite flushing is expensive and is driven from the plugin
lifecycle, not from register():
// composition root
$lifecycle->onActivate(function () use ($public): void {
$public->register(); // add the rules this request…
$public->flushRules(); // …then persist them
});
$lifecycle->onDeactivate(fn () => $public->flushRules());
Table of Contents
Properties
Methods
- __construct() : mixed
- addRoute() : void
- Register a public route. $regex is a WordPress rewrite regex (matched against the request path); $handler runs on `template_redirect` when the route matches and owns its own output/exit.
- dispatch() : void
- `template_redirect` action callback: invoke the matched route handler, or do nothing (letting WordPress render the normal template) when no route matched this request.
- flushRules() : void
- Persist the current rewrite rule set. Expensive — drive it from the plugin activation/deactivation lifecycle only (see the class docblock), never on a normal request.
- getRegisteredRoutes() : array<string, string>
- queryVar() : string
- The per-component query var name that carries the matched route name.
- register() : void
- Wire the front-end routing on a normal request (call from `init`): expose the query var, add a rewrite rule per route, and hook the dispatcher.
- registerQueryVar() : array<int, string>
- `query_vars` filter callback: make the component's route var public.
Properties
$queryVar read-only
private
string
$queryVar
$routes
private
array<string, Closure}>
$routes
= []
Methods
__construct()
public
__construct(HostComponentContextInterface $context) : mixed
Parameters
- $context : HostComponentContextInterface
addRoute()
Register a public route. $regex is a WordPress rewrite regex (matched against the request path); $handler runs on `template_redirect` when the route matches and owns its own output/exit.
public
addRoute(string $name, string $regex, callable $handler) : void
Parameters
- $name : string
- $regex : string
- $handler : callable
dispatch()
`template_redirect` action callback: invoke the matched route handler, or do nothing (letting WordPress render the normal template) when no route matched this request.
public
dispatch() : void
flushRules()
Persist the current rewrite rule set. Expensive — drive it from the plugin activation/deactivation lifecycle only (see the class docblock), never on a normal request.
public
flushRules() : void
getRegisteredRoutes()
public
getRegisteredRoutes() : array<string, string>
Return values
array<string, string> —route name => rewrite regex
queryVar()
The per-component query var name that carries the matched route name.
public
queryVar() : string
Return values
stringregister()
Wire the front-end routing on a normal request (call from `init`): expose the query var, add a rewrite rule per route, and hook the dispatcher.
public
register() : void
registerQueryVar()
`query_vars` filter callback: make the component's route var public.
public
registerQueryVar(array<int, string> $vars) : array<int, string>
Parameters
- $vars : array<int, string>