Conditions and Actions
Acorn MilliRules ships with three route-aware conditions and two HTTP response actions. These are registered automatically by the Acorn package and available in the fluent builder immediately.
Conditions#Copied!
Route Name#Copied!
Match the current Laravel route name. Useful for targeting named routes like docs.show or groups like docs.*.
1Rules::create('docs-headers')
2 ->when()
3 ->routeName('docs.show')
4 ->then()
5 ->setHeader('X-Docs', 'true')
6 ->register();
Pattern matching with the LIKE operator:
1Rules::create('all-docs-headers')
2 ->when()
3 ->routeName('docs.*', 'LIKE')
4 ->then()
5 ->setHeader('X-Section', 'docs')
6 ->register();
For a complete list of operators and examples, see the Route Name reference.
Route Parameter#Copied!
Check the value of a named route parameter. The first argument is the parameter name, the second is the expected value.
1Rules::create('millicache-product-header')
2 ->when()
3 ->routeParameter('product', 'millicache')
4 ->then()
5 ->setHeader('X-Product', 'millicache')
6 ->register();
When only a parameter name is provided (no value), the condition checks if the parameter exists:
1Rules::create('has-product-parameter')
2 ->when()
3 ->routeParameter('product')
4 ->then()
5 ->setHeader('X-Has-Product', 'true')
6 ->register();
For existence checks, pattern matching, and all operators, see the Route Parameter reference.
Route Controller#Copied!
Match the fully-qualified controller class name handling the current route.
1Rules::create('docs-controller-headers')
2 ->when()
3 ->routeController('AppHttpControllersDocsController')
4 ->then()
5 ->setHeader('X-Handler', 'docs')
6 ->register();
Partial matching with LIKE:
1Rules::create('any-docs-controller')
2 ->when()
3 ->routeController('*DocsController', 'LIKE')
4 ->then()
5 ->setHeader('X-Handler', 'docs')
6 ->register();
For all operators and examples, see the Route Controller reference.
Actions#Copied!
Set Header#Copied!
Add an HTTP response header. The first argument is the header name, the second is the value.
1Rules::create('security-headers')
2 ->when()
3 ->routeName('docs.*', 'LIKE')
4 ->then()
5 ->setHeader('X-Content-Type-Options', 'nosniff')
6 ->setHeader('X-Frame-Options', 'DENY')
7 ->setHeader('Referrer-Policy', 'strict-origin-when-cross-origin')
8 ->register();
Headers support placeholders that resolve from context:
1Rules::create('product-header')
2 ->when()
3 ->routeParameter('product')
4 ->then()
5 ->setHeader('X-Product', '{route.parameters.product}')
6 ->register();
For full details on placeholder support and behavior, see the Set Header reference.
Redirect#Copied!
Redirect the request to a different URL. The first argument is the target URL, the second (optional) is the HTTP status code (default: 302).
1Rules::create('legacy-docs-redirect')
2 ->when()
3 ->routeName('docs.legacy')
4 ->then()
5 ->redirect('/docs', 301)
6 ->register();
Redirects support placeholders for dynamic URLs:
1Rules::create('product-redirect')
2 ->when()
3 ->routeName('docs.old-product')
4 ->then()
5 ->redirect('/docs/{route.parameters.product}/latest', 301)
6 ->register();
For full details on redirect behavior and examples, see the Redirect reference.
Combining Acorn and Core Conditions#Copied!
You can mix Acorn route conditions with conditions from other MilliRules packages (like the PHP package's request_url or cookie conditions) in the same rule:
1Rules::create('docs-logged-in-redirect')
2 ->when()
3 ->routeName('docs.premium')
4 ->cookie('session_token', '', '!=')
5 ->then()
6 ->setHeader('X-Access', 'premium')
7 ->register();
By default, all conditions must match (match_type: all). To match when any condition is true, use matchAny():
1Rules::create('product-pages')
2 ->matchAny()
3 ->when()
4 ->routeName('products.show')
5 ->routeParameter('product', 'millicache')
6 ->then()
7 ->setHeader('X-Product-Page', 'true')
8 ->register();
request_url, cookie, request_method), see the MilliRules Conditions Reference and Actions Reference.Next Steps#Copied!
- Artisan Commands — list, inspect, and scaffold rules from the CLI
- Custom Conditions and Actions — create your own condition and action types
- Conditions Reference — full reference for all Acorn conditions
- Actions Reference — full reference for all Acorn actions