PHP code example of daycry / maintenancemode

1. Go to this page and download the library: Download daycry/maintenancemode library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

daycry / maintenancemode example snippets



// 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';

// 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

Events::on( 'pre_system', 'Daycry\Maintenance\Controllers\Maintenance::check' );

public $aliases = [
    'maintenance' => \Daycry\Maintenance\Filters\Maintenance::class,
    ...
]

public $globals = [
    'before' => [
        'maintenance',
        ...
    ],
    'after'  => [
        ...
    ],
];

// Allow specific IPs to bypass maintenance
$config->allowedIps = ['192.168.1.100', '203.0.113.0'];

// Set cookie programmatically
setcookie('maintenance_bypass', 'your-secret-key', time() + 3600);

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
    });
}



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)
}

// 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/*']]
    ]
];

// app/Config/Maintenance.php
public string $logLevel = 'debug';
public bool $enableLogging = true;
bash
php spark mm:publish
bash
# 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
bash
php spark mm:status
bash
php spark mm:up
bash
php spark mm:status

app/Views/errors/html/error_503.php
bash
# 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
bash
# Force migration with cleanup
php spark mm:migrate --to=cache --force

# Check file permissions
ls -la writable/maintenance/