A powerful, modern and highly configurable maintenance mode library for CodeIgniter 4 with intelligent caching, modern UI, and advanced features.
- π High Performance: Intelligent cache-based storage system (10x faster than file-based)
- π¨ Modern UI: Responsive, dark-mode compatible maintenance page with auto-refresh
- π Multiple Bypass Methods: IP whitelist, cookies, and secret URL bypass
- π Comprehensive Logging: Complete audit trail of maintenance events
- βοΈ Highly Configurable: Extensive configuration options for all scenarios
- π Scalable: Works perfectly in distributed environments
- π Backward Compatible: 100% compatible with existing implementations
- π± Mobile-Friendly: Fully responsive design with accessibility features
Install via Composer:
composer require daycry/maintenancemode
php spark mm:publish
This creates app/Config/Maintenance.php
with all available options.
<?php
// app/Config/Maintenance.php
public bool $useCache = true; // Use cache instead of files (recommended)
public bool $enableLogging = true; // Log maintenance events
public string $defaultMessage = 'We are currently performing maintenance...';
public bool $allowSecretBypass = true; // Enable secret URL bypass
public string $secretBypassKey = 'your-secret-key';
For optimal performance, configure your cache in app/Config/Cache.php
:
// Redis (recommended for production)
public array $redis = [
'handler' => 'redis',
'host' => '127.0.0.1',
'port' => 6379,
// ... other settings
];
// Or use any other cache driver (Memcached, File, etc.)
php spark mm:down
php spark mm:status
php spark mm:up
Create new event in app/Config/Events.php
Events::on( 'pre_system', 'Daycry\Maintenance\Controllers\Maintenance::check' );
edit application/Config/Filters.php and add the new line in $aliases array:
public $aliases = [
'maintenance' => \Daycry\Maintenance\Filters\Maintenance::class,
...
]
and add "maintenance" in $globals['before'] array:
public $globals = [
'before' => [
'maintenance',
...
],
'after' => [
...
],
];
# Basic activation
php spark mm:down
# With custom message
php spark mm:down --message="We'll be back in 30 minutes!"
# With IP whitelist (multiple IPs supported)
php spark mm:down --allow=192.168.1.100,203.0.113.0
# With automatic expiry (30 minutes)
php spark mm:down --duration=30
# With secret bypass key
php spark mm:down --secret=my-secret-123
php spark mm:status
This displays a comprehensive status table with:
- π΄/π’ Current status indicator
- π Storage method (cache/file)
- π Configuration summary
- π Allowed IPs and bypass keys
- π Real-time bypass detection
- π¦ Current access status
- π‘ Practical usage tips
The status command now shows real-time bypass information:
π Current Bypass Status:
π Config Secret available (add ?maintenance_secret=your-key to URL)
β
Data Secret (via URL parameter)
π IP Address bypass configured (current IP 192.168.1.200 not in allowed list)
πͺ Cookie bypass configured (cookie not set or invalid)
π¦ Access Status from CLI:
β
Access ALLOWED: CLI access (always allowed)
π‘ Tips:
β’ Add your IP: php spark mm:down --allow=192.168.1.200
β’ Use secret: php spark mm:down --secret=your-key
β’ Access URL: https://yoursite.com?maintenance_secret=your-key
Key Features:
- β Active bypass detection: Shows which bypass methods are currently working
- π Real-time status: Indicates if current user would have access
- π‘ Practical guidance: Provides specific commands to enable access
- π― Priority indication: Shows bypass method priority order
- π± Current IP display: Shows your current IP for whitelist setup
php spark mm:up
Switch between file and cache storage without losing configuration:
# Migrate from files to cache
php spark mm:migrate --to=cache
# Migrate from cache to files
php spark mm:migrate --to=file
The enhanced mm:status
command provides comprehensive bypass detection:
php spark mm:status
What it shows:
- π’ Active bypass methods: Currently working bypass options
- π΄ Inactive bypass methods: Configured but not currently active
- π― Current access status: Whether you would have access right now
- π‘ Actionable tips: Specific commands to enable access
- π Configuration overview: All bypass methods configured
The system checks bypass methods in this priority order:
- π₯ Config Secret (
app/Config/Maintenance.php
) - π₯ Data Secret (set when activating maintenance)
- π₯ IP Address (IP whitelist)
- π Cookie (automatically generated)
Scenario 1: Developer wants access
# Check current status
php spark mm:status
# Example output shows:
# π IP Address bypass configured (current IP 192.168.1.200 not in allowed list)
# π‘ Add your IP: php spark mm:down --allow=192.168.1.200
# Add your IP to allowed list
php spark mm:down --allow=192.168.1.200
Scenario 2: Share access via secret URL
# Check secret information
php spark mm:status
# Example output shows:
# π Secret Bypass Information:
# URL: https://yoursite.com?maintenance_secret=abc123
# Share this URL with authorized users
Scenario 3: Troubleshooting access issues
# Check why access is blocked
php spark mm:status
# Example output shows:
# β Access BLOCKED: No valid bypass method
# π‘ Tips:
# β’ Add your IP: php spark mm:down --allow=YOUR_IP
# β’ Use secret: php spark mm:down --secret=your-key
IP-based Bypass:
// Allow specific IPs to bypass maintenance
$config->allowedIps = ['192.168.1.100', '203.0.113.0'];
Secret URL Bypass:
https://yoursite.com/any-page?maintenance_secret=your-secret-key
Cookie-based Bypass:
// Set cookie programmatically
setcookie('maintenance_bypass', 'your-secret-key', time() + 3600);
Override the default maintenance page by creating:
app/Views/errors/html/error_503.php
The modern template includes:
- π¨ Responsive design with CSS variables
- π Auto-refresh functionality
- π Dark mode support
- βΏ Accessibility features
- π± Mobile optimization
use Daycry\MaintenanceMode\Libraries\MaintenanceStorage;
$storage = new MaintenanceStorage();
// Check if maintenance is active
if ($storage->isActive()) {
// Get maintenance data
$data = $storage->getData();
echo "Message: " . $data['message'];
}
// Activate programmatically
$storage->save([
'message' => 'Custom maintenance message',
'allowedIps' => ['192.168.1.100'],
'time' => time(),
'secret' => 'custom-secret'
]);
// Deactivate
$storage->remove();
// In your BaseController or specific controllers
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
// Add custom logic before maintenance check
Events::on('pre_maintenance_check', function () {
// Your custom logic here
});
}
Storage Method | Average Response Time | Memory Usage | Scalability |
---|---|---|---|
Cache (Redis) | ~0.5ms | Low | βββββ |
Cache (Memcached) | ~0.8ms | Low | βββββ |
Cache (File) | ~2ms | Medium | βββ |
File Storage | ~5ms | Medium | ββ |
Recommendation: Use Redis cache for production environments with high traffic.
Complete configuration options in app/Config/Maintenance.php
:
<?php
namespace Config;
use CodeIgniter\Config\BaseConfig;
class Maintenance extends BaseConfig
{
// Storage Configuration
public bool $useCache = true; // Use cache vs files
public string $cacheKey = 'maintenance_mode'; // Cache key name
public int $cacheTTL = 86400; // Cache TTL (24 hours)
// File Storage
public string $driver = 'file'; // Storage driver
public string $filePath = WRITEPATH . 'maintenance/maintenance.json';
// UI & Messages
public string $defaultMessage = 'We are currently performing maintenance. Please try again later.';
public int $httpCode = 503; // HTTP status code
public int $autoRefreshSeconds = 30; // Auto-refresh interval
// Security & Access
public array $allowedIps = []; // IP whitelist
public bool $allowSecretBypass = true; // Enable secret bypass
public string $secretBypassKey = ''; // Default secret key
public string $bypassCookieName = 'maintenance_bypass';
// Logging & Monitoring
public bool $enableLogging = true; // Log maintenance events
public string $logLevel = 'info'; // Log level
// Advanced Features
public bool $showRetryAfter = true; // Show Retry-After header
public int $retryAfter = 3600; // Retry-After value (1 hour)
}
Add the maintenance filter to your routes:
// app/Config/Routes.php
$routes->group('/', ['filter' => 'maintenance'], static function ($routes) {
$routes->get('/', 'Home::index');
$routes->get('about', 'Pages::about');
// Add other routes that should be checked
});
// Or apply globally in app/Config/Filters.php
public array $globals = [
'before' => [
'maintenance' => ['except' => ['admin/*', 'api/*']]
]
];
Run the test suite:
# Run all tests
composer test
# Run with coverage
composer test:coverage
# Run specific test
./vendor/bin/phpunit tests/Maintenance/CommandsTest.php
1. Cache not working:
# Check cache configuration
php spark cache:info
# Clear cache
php spark cache:clear
# Test cache connection
php spark mm:status
2. IP bypass not working:
# Check your real IP
php spark mm:down --allow=$(curl -s ifconfig.me)
# Or check current IP in logs
tail -f writable/logs/log-*.php | grep maintenance
3. Migration issues:
# Force migration with cleanup
php spark mm:migrate --to=cache --force
# Check file permissions
ls -la writable/maintenance/
Enable debug logging:
// app/Config/Maintenance.php
public string $logLevel = 'debug';
public bool $enableLogging = true;
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature
- Run tests:
composer test
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
- β¨ New: Cache-based storage system with 10x performance improvement
- β¨ New: Modern responsive maintenance page with dark mode
- β¨ New: Migration command for seamless storage switching
- β¨ New: Enhanced CLI commands with validation and better UX
- β¨ New: Comprehensive logging and monitoring
- π§ Enhanced: Expanded configuration with 12+ new options
- π§ Enhanced: Better error handling and validation
- π Fixed: Multiple IP handling and secret bypass improvements
- Basic file-based maintenance mode
- Simple CLI commands
- Basic IP filtering
This project is licensed under the MIT License - see the LICENSE file for details.
- π§ Email: support@daycry.com
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
- β Buy me a coffee: PayPal
Made with β€οΈ for the CodeIgniter 4 community