Route Context is a powerful, automated metadata system in the Anchor Framework that allows the application to "understand" its purpose at any given point in the request lifecycle. It powers a suite of Smart Features that reduce boilerplate and ensure consistency across your application.
The framework automatically hydrants the Route Context as soon as a request is resolved to a controller. It extracts standard identifiers from the controller's namespace and method:
- Namespace:
App\{Domain}\Controllers\{Entity}Controller - Method:
{Action}
For example, App\Account\Controllers\UserController@edit results in:
domain:Accountentity:Userresource:users(auto-pluralized)action:editvalidator:App\Account\Validations\Form\UserFormRequestValidation(explicit override)
The Route Context system currently powers five major "Smart Features" across the framework:
Automatically resolves and executes the correct RequestValidation class based on current context. No need to manually instantiate or call validators in your controllers.
Automatically flushes cache tags for the current entity whenever a state-changing request (POST/PUT/DELETE) is successful.
Generates human-readable audit trails (e.g., "EDIT action on User in Account") automatically for every state-changing action.
Allows locking down specific resources (e.g., "maintenance for the 'payments' resource") without taking the entire application offline.
Generates SEO-friendly page titles (e.g., "Edit User") and social metadata automatically based on the active resource and action.
You can access or modify the context at any time via the Request object:
// Get specific context
$resource = $request->getRouteContext('resource');
// Manually set or override context
$request->setRouteContext('custom_tag', 'value');Context is also available in your views for dynamic UI adjustments:
<?php if ($this->isResourceContext('users')): ?>
<!-- Active menu state logic -->
<?php endif; ?>Instead of manually checking strings, Anchor provides fluent helpers in view templates:
$this->isResourceContext('users'): Checks ifresourceisusers.$this->isActionContext('edit'): Checks ifactionisedit.$this->isDomainContext('Account'): Checks ifdomainisAccount.$this->isEntityContext('User'): Checks ifentityisUser.$this->context('key', 'default'): Retrieves any context value.
You can explicitly define the route context in App/Config/route.php. This is useful for "Zero-Controller" setups where you want to set the context without any PHP logic.
Standard Anchor routes resolve following a {domain}/{entity}/{action} pattern. For example, auth/login would normally resolve to:
- Domain: Auth
- Entity: Login
- Resource: logins
- Action: index
However, for a clean UI, you might want it to be treated as part of an "Identity" resource. By using a proactive override, you can force this mapping:
return [
'context' => [
'auth/login' => [
'resource' => 'identity',
'action' => 'login',
],
],
];- Routing Phase: When the
UrlResolvermatches a URL, it checks if a corresponding key exists in theroute.contextconfiguration. - Injection: If found, these values are attached to the
RouteMatchobject. - Application Phase: During
App::handle(), thehydrateRouteContext()method is called. It first performs standard discovery (based on controller name) and then merges the explicit overrides, ensuring the config values take precedence. - Availability: By the time your middleware or controller executes,
$request->getRouteContext()will return the overridden values.
The dynamic hydration logic in App.php automatically identifies the domain, entity, resource, and action based on the controller's namespace and method name if no explicit override is provided.