Search code examples
phplaravellaravel-5.6laravel-artisan

Laravel 5.6 Bring site up without commands


I have a Laravel 5.6 website where I want this functionality to be enabled for a non-technical admin, so that he can bring the website down or up at any point of time.

I have successfully down the website by using

    Route::get('shut/down', function() {
        `Artisan::call('down');`
    });

But when I want my application to back up using this

Route::get('bring/the/application/back/up', function() 
{
    Artisan::call('up');
});

But this isn't working because my website is already down so this won't work. But in command line we have some commands through which we can Exclude IP addresses for maintenance mode.


Example : php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16

Do we have any workaround to exclude some selected IP addresses without command line method or bring the site back up without using commands ?


Solution

  • You have to give a depper look at the Official documentation where it explains how to programmatically call the commands:

    Sometimes you may wish to execute an Artisan command outside of the CLI. For example, you may wish to fire an Artisan command from a route or controller. You may use the call method on the Artisan facade to accomplish this. The call method accepts either the command's name or class as the first argument, and an array of command parameters as the second argument. The exit code will be returned:

    Route::get('/foo', function () {
        $exitCode = Artisan::call('email:send', [
            'user' => 1, '--queue' => 'default'
        ]);
    
        //
    });
    

    So, in your case you have to update your route callback:

    Route::get('shut/down', function() {
        Artisan::call('email:send', [
            '--allow' => 'xxxx.xxxx.xxxx.xxxx' // Your ip address
        ]);
    });
    

    In this way, your ip address will be enabled to access to the bring/the/application/back/up address. Anyway I would look for a different solution if you just want to simply "hide" the fronted, by creating a specific variable (configuration, database, whatever) that "hides" the website but keeps up the admin panel in order to activate/deactivate in a more easy way.