Introduction to MilliRules

MilliRules is a powerful, flexible rule engine for PHP and WordPress that lets you create conditional logic using an elegant fluent API. Whether you're building a WordPress plugin or a framework-agnostic PHP application, MilliRules makes it easy to implement complex business rules without tangling your code with if-else statements.

What is MilliRules?#Copied!

MilliRules allows you to define rules that automatically execute actions when specific conditions are met. Think of it as a sophisticated "if-then" system that:

  • Separates logic from code - Define rules independently of your application logic
  • Works everywhere - Use in WordPress, Laravel, Symfony, or any PHP 7.4+ project
  • Provides a fluent API - Write readable, chainable code that's easy to understand
  • Extends easily - Add custom conditions, actions, and packages for your needs

Why Use MilliRules?#Copied!

Clean, Declarative Code#Copied!

Instead of scattering conditional logic throughout your codebase:

 1// Traditional approach - logic mixed with implementation
 2if (is_admin() && is_user_logged_in() && $_SERVER['REQUEST_URI'] === '/wp-admin/settings.php') {
 3    if (current_user_can('manage_options')) {
 4        do_action('my_admin_action');
 5        update_option('last_settings_access', time());
 6        error_log('Admin accessed settings');
 7    }
 8}

With MilliRules, you define rules declaratively:

 1// MilliRules approach - clean and declarative
 2Rules::create('log_settings_access', 'wp')
 3    ->title('Log Settings Page Access')
 4    ->when()
 5        ->request_url('/wp-admin/settings.php')
 6        ->is_user_logged_in()
 7        ->user_can('manage_options')
 8    ->then()
 9        ->custom('trigger_admin_action')
10        ->custom('update_last_access')
11        ->custom('log_access')
12    ->register();

Key Benefits#Copied!

  1. Maintainability - Rules are self-contained and easy to understand, update, or remove
  2. Reusability - Define conditions and actions once, use them across multiple rules
  3. Testability - Test rules in isolation without complex setup
  4. Flexibility - Dynamically register or unregister rules based on runtime conditions
  5. Organization - Group related rules together, control execution order
  6. Extensibility - Create custom conditions, actions, and packages tailored to your needs

When to Use MilliRules#Copied!

MilliRules is ideal for:

WordPress Development#Copied!

  • Content Filtering - Modify content based on user roles, post types, or custom conditions
  • Access Control - Restrict or grant access to pages, features, or content
  • Caching Logic - Apply cache headers based on request patterns
  • Feature Flags - Enable/disable features based on environment or user attributes
  • Admin Customization - Modify admin behavior based on user roles or contexts

PHP Applications#Copied!

  • API Rate Limiting - Apply rate limits based on user tiers or endpoints
  • Request Routing - Route requests based on complex conditions
  • Data Validation - Apply validation rules based on context
  • Business Logic - Implement business rules that change frequently
  • Event Handling - Trigger actions based on application events

Core Components#Copied!

MilliRules is built around four main concepts:

flowchart LR
    subgraph Rules["Rules"]
        R["Combines conditions + actions<br/>with metadata"]
    end

    subgraph Conditions["Conditions"]
        C["Define WHEN<br/>to execute"]
    end

    subgraph Actions["Actions"]
        A["Define WHAT<br/>happens"]
    end

    subgraph Packages["Packages"]
        P["Provide conditions,<br/>actions & context"]
    end

    Packages --> Conditions
    Packages --> Actions
    Conditions --> Rules
    Actions --> Rules

1. Rules#Copied!

The foundation of MilliRules - a rule combines conditions and actions with metadata like title, order, and enabled status.

2. Conditions#Copied!

Define when a rule should execute. MilliRules provides built-in conditions for URLs, HTTP methods, cookies, constants, and WordPress-specific checks.

3. Actions#Copied!

Define what happens when conditions are met. Actions can be simple callbacks, class methods, or complex custom implementations.

4. Packages#Copied!

Modular functionality bundles that provide conditions, actions, context providers, and placeholder resolvers. MilliRules comes with PHP and WordPress packages out of the box.

How It Works#Copied!

The MilliRules execution flow:

flowchart LR
    Init["1. Initialize"] --> Register["2. Register Rules"]
    Register --> Execute["3. Execute"]
    Execute --> Evaluate["4. Evaluate Conditions"]
    Evaluate -->|"match"| Actions["5. Run Actions"]
    Evaluate -->|"no match"| Skip["Skip Rule"]
  1. Initialization - MilliRules::init() registers and loads packages
  2. Rule Registration - Rules are created and registered using the fluent API
  3. Execution - Rules execute automatically (via WordPress hooks) or manually
  4. Condition Evaluation - Each rule's conditions are evaluated against the current context
  5. Action Execution - When conditions match, the rule's actions execute in sequence

When NOT to Use MilliRules#Copied!

MilliRules might be overkill for:

  • Simple one-time checks - A basic if statement is often sufficient
  • Performance-critical hot paths - The rule engine adds minimal but measurable overhead
  • Extremely simple applications - If you only need 1-2 conditional checks, MilliRules might be unnecessary

Using Acorn / Roots?#Copied!

If you're building on the Roots stack with Acorn, check out the Acorn MilliRules companion package. It adds route-aware conditions, HTTP response actions, Artisan commands, and automatic rule discovery — everything you need to define and manage rules that react to Laravel routes.

 1composer require millipress/acorn-millirules

Getting Started#Copied!

Ready to start using MilliRules? Follow these steps:

  1. Quick Start - Install and initialize MilliRules in minutes
  2. Your First Rule - Create your first rule with a hands-on tutorial
  3. Core Concepts - Deep dive into architecture and concepts

Learn More#Copied!

Core Documentation#Copied!

Customization#Copied!

Reference#Copied!


Ready to get started? Continue to Quick Start Guide to install and initialize MilliRules.