php - Runtime configuration for Symfony application

291

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 acache: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 usegetenv() 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?

341

Answer

Solution:

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.

441

Answer

Solution:

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

People are also looking for solutions to the problem: php - CodeIgniter Captcha Image name

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.