Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime configuration for Symfony application

Tags:

php

symfony

I would like to add a "maintenance mode" to a Symfony application - a simple boolean configuration which I set during runtime. Controllers (or possibly the front controller) can check this value and either; process the request as normal, or return HTTP 503 and a friendly message.

I can think of 3 possible solutions:

  • parameters.yml This is the logical place to put a configuration, but it gets cached by the Symfony app and using maintenance mode may require a cache:clear to take effect. I would like to avoid this!

  • A custom configuration file It could look for the existence of "maintenance.flag" or read a setting in a custom file. This is adding an extra disk operation for every controller visit and feels inefficient.

  • An environment variable It could use getenv() to look for the existence and value of a maintenance mode environment variable. This feels like an efficient approach and I can't think of any negatives.

Has anyone found an efficient way to achieve this kind of feature? Am I re-inventing the wheel?

like image 602
NoChecksum Avatar asked Dec 07 '25 23:12

NoChecksum


2 Answers

There's a very good symfony bundle. Check this out

LexikMaintenanceBundle

You just install it in your project and then you can put your site in maintenance mode with just a command line.

You can set up many configurations like the duration of this state, custom error page and so on.

like image 117
Omar Alves Avatar answered Dec 09 '25 13:12

Omar Alves


as you asked about runtime and i suggested to use webserver conf have a look at this .htaccess approach which looks if there is a maintenance.html at document_root and if so it will redirect all requests to it and serve the correct status code

    RewriteEngine On

    # Maintenance mode if /maintenance.html is present
    RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /maintenance.html [R=503,L]
    ErrorDocument 503 /maintenance.html

so means if you want to activate maintenance mode just upload the file to you webdir

like image 28
john Smith Avatar answered Dec 09 '25 14:12

john Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!