Configuration

The configuration file controls how the MilliRules middleware is registered. Publish it with:

 1wp acorn vendor:publish --tag=millirules

This creates config/millirules.php in your application.

Full Config Reference#Copied!

 1<?php
 2
 3return [
 4
 5    /*
 6    |--------------------------------------------------------------------------
 7    | Middleware
 8    |--------------------------------------------------------------------------
 9    |
10    | Control how the MilliRules middleware is registered. The middleware
11    | executes rules after route matching and applies response modifications
12    | (headers, redirects) to the outgoing HTTP response.
13    |
14    */
15
16    'middleware' => [
17
18        // Set to false to disable automatic middleware registration.
19        'enabled' => true,
20
21        // Middleware groups to attach to (e.g. ['web', 'api']).
22        'groups' => ['web'],
23    ],
24
25];

Options#Copied!

Key Type Default Description
middleware.enabled bool true Enable or disable automatic middleware registration. Set to false to register the middleware manually.
middleware.groups string[] ['web'] Middleware groups the ExecuteRules middleware is pushed to. Rules will only execute on routes belonging to these groups.

Adding Middleware Groups#Copied!

To execute rules on API routes as well:

 1'middleware' => [
 2    'enabled' => true,
 3    'groups' => ['web', 'api'],
 4],

Disabling Automatic Middleware#Copied!

Set middleware.enabled to false to take full control over where the middleware runs:

 1'middleware' => [
 2    'enabled' => false,
 3    'groups' => [],
 4],

Then register the middleware manually on specific routes or groups:

 1use MilliRulesAcornHttpMiddlewareExecuteRules;
 2
 3// On a specific route
 4Route::get('/docs/{product}', [DocsController::class, 'show'])
 5    ->middleware(ExecuteRules::class);
 6
 7// On a route group
 8Route::middleware([ExecuteRules::class])->group(function () {
 9    Route::get('/docs/{product}', [DocsController::class, 'show']);
10    Route::get('/docs/{product}/{path}', [DocsController::class, 'page']);
11});
Tip: Manual registration is useful when you only want rules to execute on a subset of routes, avoiding the overhead of rule evaluation on routes that never match any conditions.

Customizing Stubs#Copied!

After publishing, you can customize the stub templates used by the scaffolding commands. Published stubs are located at:

Stub Path
Rule stubs/millirules/rule.stub
Action stubs/millirules/action.stub
Condition stubs/millirules/condition.stub

Published stubs take priority over the package defaults. The scaffolding commands check for a published stub first and fall back to the package stub if none is found.

Available Placeholders#Copied!

Placeholder Replaced with
{{ namespace }} The generated class namespace (e.g., AppRules)
{{ class }} The generated class name (e.g., SecurityHeaders)
{{ ruleId }} Kebab-case rule ID, rule stubs only (e.g., security-headers)
{{ type }} Snake_case type name, action and condition stubs only (e.g., cors_headers)

Example: Customized Rule Stub#Copied!

 1<?php
 2
 3namespace {{ namespace }};
 4
 5use MilliRulesRules;
 6
 7class {{ class }}
 8{
 9    public function register(): void
10    {
11        Rules::create('{{ ruleId }}')
12            ->order(10)
13            ->when()
14                // Add conditions here
15            ->then()
16                // Add actions here
17            ->register();
18    }
19}

Next Steps#Copied!