Route Context Reference
The Acorn package provides a route context that exposes metadata about the currently matched Laravel route. This context is used by route conditions internally and can be accessed in custom conditions, custom actions, and placeholders.
Context Keys#Copied!
The route context is loaded via $context->load('route') and provides the following keys:
| Key | Type | Description | Example |
|---|---|---|---|
route.name |
string |
The named route identifier | 'docs.show' |
route.parameters |
array |
All route parameters as key-value pairs | ['product' => 'millicache', 'path' => 'getting-started'] |
route.parameters.{name} |
string |
A specific route parameter by name | 'millicache' |
route.controller |
string |
The fully-qualified controller class name | 'AppHttpControllersDocsController' |
route.action |
string |
The controller method name | 'show' |
route.uri |
string |
The route URI pattern (with placeholders) | '/docs/{product}/{path?}' |
route.middleware |
array |
Middleware applied to the route | ['web', 'auth'] |
Route Name#Copied!
The route name as defined by Route::name() in your routes file. Returns an empty string if the route is unnamed.
1// Route definition
2Route::get('/docs/{product}', [DocsController::class, 'show'])->name('docs.show');
3
4// Context value
5$context->get('route.name'); // 'docs.show'
Route Parameters#Copied!
All resolved route parameters as an associative array. Individual parameters can be accessed using dot notation:
1// Route definition
2Route::get('/docs/{product}/{path?}', [DocsController::class, 'show']);
3
4// Visiting: /docs/millicache/getting-started
5$context->get('route.parameters'); // ['product' => 'millicache', 'path' => 'getting-started']
6$context->get('route.parameters.product'); // 'millicache'
7$context->get('route.parameters.path'); // 'getting-started'
Controller and Action#Copied!
The controller class name and method are extracted from the route's uses action:
1// Route definition: DocsController@show
2$context->get('route.controller'); // 'AppHttpControllersDocsController'
3$context->get('route.action'); // 'show'
4
5// Invokable controller: DocsController (no @method)
6$context->get('route.controller'); // 'AppHttpControllersDocsController'
7$context->get('route.action'); // ''
URI Pattern#Copied!
The raw route URI pattern with parameter placeholders intact:
1$context->get('route.uri'); // '/docs/{product}/{path?}'
Middleware#Copied!
An array of middleware names or classes applied to the route:
1$context->get('route.middleware'); // ['web', 'auth']
Using Context in Placeholders#Copied!
Action arguments support {context.key} placeholders that are resolved at execution time. All route context keys are available:
1Rules::create('dynamic-redirect')
2 ->when()
3 ->routeParameter('product')
4 ->then()
5 ->redirect('/new-docs/{route.parameters.product}', 301)
6 ->register();
7
8Rules::create('dynamic-header')
9 ->when()
10 ->routeParameter('product')
11 ->then()
12 ->setHeader('X-Product', '{route.parameters.product}')
13 ->setHeader('X-Route', '{route.name}')
14 ->register();
Using Context in Custom Code#Copied!
In custom conditions and actions, load and access the route context through the Context object:
1use MilliRulesContext;
2
3// In a custom condition's get_actual_value() or action's execute()
4protected function get_actual_value(Context $context): string
5{
6 // Load route context (idempotent — safe to call multiple times)
7 $context->load('route');
8
9 // Read a value with a default
10 $name = $context->get('route.name', '');
11
12 return is_string($name) ? $name : '';
13}
1use MilliRulesContext;
2
3// In a custom action
4public function execute(Context $context): void
5{
6 $context->load('route');
7
8 $product = $context->get('route.parameters.product', '');
9
10 if (is_string($product) && $product !== '') {
11 app('millirules.response')->addHeader('X-Product', $product);
12 }
13}
$context->load('route') before accessing route keys. You can use $context->get('route') directly. The load is lazy — route data is built only on the first call and cached for subsequent access.Empty Context Behavior#Copied!
When there is no matched Laravel route (e.g., a 404 page or a request handled outside the router), the route context returns empty defaults:
| Key | Empty value |
|---|---|
route.name |
'' (empty string) |
route.parameters |
[] (empty array) |
route.parameters.{name} |
'' (empty string, via default) |
route.controller |
'' (empty string) |
route.action |
'' (empty string) |
route.uri |
'' (empty string) |
route.middleware |
[] (empty array) |
Route conditions will not match against empty values unless explicitly checking for empty strings. This means rules with route conditions naturally skip unmatched requests.