Quick Start Guide

This guide will help you install and initialize MilliRules in just a few minutes. Whether you're using WordPress or a standalone PHP application, you'll be ready to create your first rule quickly.

Prerequisites#Copied!

Before installing MilliRules, ensure you have:

  • PHP 7.4 or higher
  • Composer - For dependency management
  • (Optional) WordPress 5.0+ - If using WordPress-specific features

Installation via Composer#Copied!

MilliRules is installed via Composer. Run this command in your project directory:

 1composer require MilliPress/MilliRules

This will download MilliRules and all its dependencies into your vendor/ directory.

Initializing MilliRules#Copied!

Before creating rules, you need to initialize MilliRules. This registers the available packages (PHP and WordPress) and prepares the rule engine.

Basic Initialization#Copied!

For most installations, whether you are using WordPress or a standalone PHP application, use the simple initialization:

 1use MilliRulesMilliRules;
 2
 3// Initialize with auto-detected packages
 4MilliRules::init();

This automatically detects your environment:

  • In WordPress, it registers and loads both the PHP and WordPress packages.
  • In Framework-agnostic environments, it automatically loads only the PHP package.

Custom Package Selection#Copied!

You can explicitly specify which packages to load:

 1use MilliRulesMilliRules;
 2
 3// Load only the PHP package (useful for early execution)
 4MilliRules::init(['PHP']);
 5
 6// Or load specific packages with custom instances
 7$custom_package = new MyCustomPackage();
 8MilliRules::init(null, [$custom_package]);
Note: The first parameter accepts package names as strings to load specific packages. The second parameter accepts PackageInterface instances to register. Using null for both parameters tells MilliRules to register and auto-load default packages.

Verifying Installation#Copied!

To verify that MilliRules is properly installed and initialized, you can check the loaded packages:

 1use MilliRulesMilliRules;
 2
 3// Initialize MilliRules
 4MilliRules::init();
 5
 6// Check loaded packages
 7$packages = MilliRules::get_loaded_packages();
 8error_log('Loaded packages: ' . print_r($packages, true));

If everything is working correctly, you should see the PHP package (and WordPress package if in WordPress environment) in your error log.

Common Pitfalls#Copied!

1. Forgetting to Initialize#Copied!

 1// ❌ Wrong - rules created before initialization
 2Rules::create('my_rule')->when()->request_url('/test')->then()->register();
 3MilliRules::init();
 4
 5// ✅ Correct - initialize first
 6MilliRules::init();
 7Rules::create('my_rule')->when()->request_url('/test')->then()->register();

2. Incorrect WordPress Hook Priority#Copied!

 1// ❌ Wrong - initializing too late
 2add_action('init', function() {
 3    MilliRules::init();
 4}, 999); // Rules may miss early hooks
 5
 6// ✅ Correct - initialize early or at top level
 7MilliRules::init();

3. Missing Autoloader#Copied!

 1// ❌ Wrong - missing autoloader
 2use MilliRulesMilliRules;
 3MilliRules::init(); // Fatal error: Class not found
 4
 5// ✅ Correct - include autoloader first
 6require_once __DIR__ . '/vendor/autoload.php';
 7use MilliRulesMilliRules;
 8MilliRules::init();

Troubleshooting#Copied!

Rules Not Executing#Copied!

  1. Check if MilliRules is initialized: Make sure MilliRules::init() is called before creating rules
  2. Verify rule is registered: Add error_log() calls to confirm your rule registration code runs
  3. Check WordPress hook timing: Ensure your rules are created before the hooks they target fire
  4. Enable debug logging: Check your error log for MilliRules-related messages

Package Not Available Error#Copied!

If you see "Package not available" errors:

  1. WordPress package: Ensure WordPress is fully loaded before initializing MilliRules
  2. Custom packages: Verify your custom package's is_available() method returns true
  3. Check dependencies: Ensure required packages are loaded first

Getting Help#Copied!

  • Review the API Reference for detailed method documentation
  • Check Real-World Examples for complete working code
  • Examine your error logs for detailed error messages

Best Practices#Copied!

  1. Initialize early - Call MilliRules::init() as early as possible in your application
  2. Check your environment - Verify PHP version and Composer are properly configured
  3. Enable error logging - Turn on error logging during development to catch issues quickly
  4. Test in isolation - Create a simple test rule to verify MilliRules is working before building complex logic

Next Steps#Copied!

Now that MilliRules is installed and initialized, you're ready to create your first rule!

Continue to Creating Your First Rule to start building with MilliRules.