Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Maintenance Bundle Laravel Package

lexik/maintenance-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require lexik/maintenance-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        Lexik\Bundle\MaintenanceBundle\LexikMaintenanceBundle::class => ['all' => true],
    ];
    
  2. Configuration: Publish the default config:

    php bin/console lexik:maintenance:config:dump
    

    Edit config/packages/lexik_maintenance.yaml to define:

    • mode (e.g., file, memcache, or database).
    • allowed_ips (e.g., ['127.0.0.1', '192.168.1.0/24']).
    • Custom error_template (optional).
  3. First Use Case: Enable maintenance mode:

    php bin/console lexik:maintenance:enable
    

    Disable it:

    php bin/console lexik:maintenance:disable
    

    Verify with curl -I http://your-site.com (should return 503).


Implementation Patterns

Core Workflows

  1. Deployment Workflow:

    • Enable maintenance mode before deploying:
      php bin/console lexik:maintenance:enable --env=prod
      
    • Deploy code/assets.
    • Disable maintenance mode after validation:
      php bin/console lexik:maintenance:disable --env=prod
      
  2. IP Whitelisting:

    • Restrict access to specific IPs (e.g., your team’s IPs) during maintenance:
      # config/packages/lexik_maintenance.yaml
      lexik_maintenance:
          allowed_ips: ['192.168.1.100', '203.0.113.5']
      
  3. Custom Error Pages:

    • Override the default 503 template by creating a custom Twig template at: templates/lexik_maintenance/error.html.twig.
    • Pass dynamic data via config/packages/lexik_maintenance.yaml:
      lexik_maintenance:
          error_template: 'custom_error_page'
          error_template_data:
              title: 'Under Maintenance'
              message: 'We’ll be back soon!'
      
  4. Database-Backed Mode:

    • Useful for shared hosting or when file/memcache isn’t an option.
    • Configure in config/packages/lexik_maintenance.yaml:
      lexik_maintenance:
          mode: database
          database_table: maintenance_mode
      
    • Run migrations (if not auto-generated):
      php bin/console doctrine:migrations:diff
      php bin/console doctrine:migrations:migrate
      
  5. Environment-Specific Configs:

    • Use separate configs for dev/prod by leveraging Symfony’s environment variables:
      # config/packages/lexik_maintenance.yaml
      when@prod:
          lexik_maintenance:
              mode: file
              file_path: '%kernel.project_dir%/var/maintenance.lock'
      
  6. Integration with CI/CD:

    • Automate maintenance mode in pipelines (e.g., GitHub Actions):
      # .github/workflows/deploy.yml
      - name: Enable Maintenance Mode
        run: php bin/console lexik:maintenance:enable --env=prod
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
      

Gotchas and Tips

Common Pitfalls

  1. Caching Issues:

    • If using memcache mode, ensure the cache server is running and accessible.
    • Clear cache after disabling maintenance:
      php bin/console cache:clear
      
  2. File Permissions:

    • For file mode, ensure the lock file (e.g., var/maintenance.lock) is writable:
      chmod 644 var/maintenance.lock
      
  3. Database Mode Quirks:

    • If the maintenance_mode table doesn’t exist, the bundle won’t work. Run migrations manually if needed.
    • Ensure your database user has permissions to read/write the table.
  4. IP Whitelisting Edge Cases:

    • CIDR notation (e.g., 192.168.1.0/24) may not work as expected if the bundle isn’t updated. Test thoroughly.
    • Localhost (127.0.0.1) is often auto-whitelisted, but verify in logs if access is denied.
  5. Symfony 5+ Compatibility:

    • The bundle is outdated (last release 2017) but may still work with Symfony 4/5. Test in a staging environment first.
    • If using Symfony 5.3+, consider forking the repo to update dependencies (e.g., symfony/http-kernel).
  6. Debugging:

    • Check logs for errors:
      php bin/console lexik:maintenance:debug
      
    • Enable verbose output for commands:
      php bin/console lexik:maintenance:enable -v
      

Pro Tips

  1. Custom Commands: Extend the bundle by creating a custom command to toggle maintenance mode with additional logic:

    // src/Command/ToggleMaintenanceCommand.php
    namespace App\Command;
    use Lexik\Bundle\MaintenanceBundle\Command\EnableCommand;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    
    class ToggleMaintenanceCommand extends EnableCommand {
        protected function execute(InputInterface $input, OutputInterface $output) {
            // Add pre/post logic (e.g., send Slack notifications)
            parent::execute($input, $output);
        }
    }
    
  2. Environment Variables: Dynamically enable/disable maintenance via environment variables:

    # .env
    MAINTENANCE_MODE=true
    
    // src/Command/EnableCommand.php (override)
    if (!$this->getContainer()->getParameter('kernel.environment') === 'prod' ||
        !$this->getInput()->getOption('force')) {
        throw new \RuntimeException('Maintenance mode can only be enabled in production.');
    }
    
  3. Logging Maintenance Events: Log enable/disable actions to track downtime:

    # config/packages/monolog.yaml
    handlers:
        maintenance:
            type: stream
            path: "%kernel.logs_dir%/maintenance.log"
            level: info
    
    // In a custom command
    $this->getContainer()->get('logger')->info('Maintenance mode enabled by ' . $this->getUser());
    
  4. Testing Maintenance Mode: Use PHPUnit to test maintenance behavior:

    // tests/Functional/MaintenanceTest.php
    public function testMaintenanceMode() {
        $client = static::createClient();
        $client->disableMaintenanceMode(); // Custom helper
        $client->request('GET', '/');
        $this->assertEquals(503, $client->getResponse()->getStatusCode());
    }
    
  5. Fallback for Shared Hosting: If no memcache/database is available, use file mode with a fallback path:

    lexik_maintenance:
        mode: file
        file_path: '%kernel.project_dir%/var/maintenance.lock'
        # Fallback if 'var' is not writable
        fallback_file_path: '/tmp/maintenance.lock'
    
  6. Security Note: Avoid exposing the maintenance toggle endpoint in production. Restrict access to the CLI commands via:

    # config/packages/security.yaml
    access_control:
        - { path: ^/bin/console, roles: ROLE_ADMIN }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor