Maintenance Mode without using Artisan?

后端 未结 5 570
日久生厌
日久生厌 2020-12-02 10:25

I\'m just wondering if anyone know\'s if there\'s a way to activate maintenance mode on a laravel website without using Artisan? I don\'t have command line access to the ser

5条回答
  •  -上瘾入骨i
    2020-12-02 10:54

    The real correct answer to question is above provided by Antonio.

    You can just call artisan from your application:

    Artisan::call('down');
    
    Artisan::call('up');
    

    Laravel 5+

    Since middleware(s) were introduced in Laravel 5, I will cover how I do it in Laravel 5.3 application.

    Create brand new middleware

    First lets create new middleware $php artisan make:middleware CheckForMaintenanceMode

    app = $app;
        }
    
        public function handle($request, Closure $next)
        {
            if ($this->app->isDownForMaintenance() && !$this->isBackendRequest($request)) {
                $data = json_decode(file_get_contents($this->app->storagePath() . '/framework/down'), true);
    
                throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']);
            }
    
            return $next($request);
        }
    
        private function isBackendRequest($request)
        {
            return ($request->is('admin/*') or $request->is('login'));
        }
    }
    

    Note: function isBackendRequest() which returns true or false if we are either in admin prefix (true) or trying to login (true) or anything else (false)

    replace global middleware

    Open up App/Http/Kernel.php and rewrite foundations middleware with our new middleware

    protected $middleware = [
        \App\Http\Middleware\CheckForMaintenanceMode::class,
    ];
    

    If application is in maintenance mode (down) we can still access login page or any admin/* page.

    Route::group(['middleware' => 'auth', 'prefix' => 'admin'], function () { 
        //admin routes
    });  
    

提交回复
热议问题