#Maintenance mode configuration

9 messages · Page 1 of 1 (latest)

bright vault
#

What is a good way to change a configuration file based on whether the application is in maintenance mode?
Let's say I have a config file with the following.

<?php

return [
    'contactFormRecipients' =>
        [
            '[email protected]', '[email protected]',
            '[email protected]'
        ]
];

Now I want something like this

use Illuminate\Support\Facades\App;

if (App::isDownForMaintenance()) {
    return [
        'contactFormRecipients' => ['[email protected]'],
    ];
}

return [
    'contactFormRecipients' => [
        '[email protected]',
        '[email protected]',
        '[email protected]',
    ],
];

However, this doesn't seem to work. I'm guessing it has to do with how the framework is booted because the App Facade in the 2nd example throws an error. When I try to run the code, I can't even get php artisan commands to execute because it says there is an error in the Facade.php file.

small echo
#

solution: don't. Just make that check in your code instead

bright vault
#

My problem is that I wouldn't be changing the code in just one place. The example I provided hinted at sending an email to the recipients of a single contact form, but my use case is actually a bit more complex. It would be nice to be able to simply swap out config files or have flags on different parts of the config file which I can turn on and off based on whether the application is in maintenance mode.

small echo
#

make a helper class/function

#

multiple config keys for each case

#

putting application logic in config file is an anti-pattern

bright vault
#

Yeah, good point about application logic in a config file.

#

Maybe something like this since I don't really want to put a bunch of logic relative to maintenance mode in my core application logic?

class SomeServiceProvider extends ServiceProvider
{
    public function register()
    {
        if ($this->app->isDownForMaintenance()) {
            $this->app->bind('contactFormRecipients', function () {
                return ['[email protected]'];
            });
        } else {
            $config = $this->app['config']['myconfigurationfile'];

            $this->app->bind('contactFormRecipients', function () use ($config) {
                return $config['contactFormRecipients'];
            });
        }
    }
mossy whale