Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php

Every time php artisan down is turned on, Laravel displays 503 page.

OK. I can customise it by creating new file called 503.blade.php inside resources/views/errors.

The point is that I don't consider Maintenance Mode as error at any point despite of the fact that it makes website unavailable for a client.

The 503 Service Unavailable error is a server-side error, meaning the problem is usually with the website's server. ... Even though the 503 Service Unavailable error means that there's an error on another computer, the issue is probably only temporary.

How can I define my own blade template (let's say maintenance_mode.blade.php) to customize what users see during the app down and leave 503 intact?

My efforts: I investigated the middleware itself inside the vendor but it only throws the exception, I assume the exception is being caught somewhere with and handles response with a corresponding view? Can someone point me on how to achieve what I need?

Thanks

like image 532
Matt Komarnicki Avatar asked Jan 04 '19 04:01

Matt Komarnicki


2 Answers

One way could be to change render method in Exception's Handler. Something like:

// app_path('Exceptions/Handler.php');

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    if ($exception instanceof \Illuminate\Foundation\Http\Exceptions\MaintenanceModeException) {
        return response()
            ->view('maintenance.down', [
                'message' => 'Come back later.'
            ], 200)
            ->header('Content-Type', 'text/html; charset=utf-8');
    }

    // in Laravel 8.x MaintenanceModeException is deprecated and one should rely on 
    // @throws \Symfony\Component\HttpKernel\Exception\HttpException
    // or
    // \Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException
    if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException ) {
        return response()
            ->view('maintenance.down', [
                'message' => 'Come back later.'
            ], 200)
            ->header('Content-Type', 'text/html; charset=utf-8');
    }

    return parent::render($request, $exception);
}

Thanks to @Ifnot 's input, answer is updated to comply Laravel 8.x

like image 127
Tpojka Avatar answered Nov 14 '22 21:11

Tpojka


If You want to display custom message in server maintanence(503.blade.php)

Laravel Has the out of box customization

php artisan down --message="We are Upgrading look and feel"

now We are Upgrading look and feel will be displayed in page while user visiting the page

If You want More customization kindly Look up the package

https://github.com/MisterPhilip/maintenance-mode

If this answer is irrelevnt or not fixed your problem kindly comment below so that i can fix that

Hope it helps

Edited

Ok then run the command in your terminal

php artisan vendor:publish and choose 0 so that it will publish all the views and configs

now open your view folder there will be a errors

folder and you can see the list of error files provided by laravel now change as per your customization and run php artisan opt:clear it will clear all the cache views ,configs and now try it

by Customising your 503.blade.php its works fine form me now

you can just view the tutorial of customising 404.blade.php and customize as per the requirement

Customize 404 in laravel

like image 43
ManojKiran Appathurai Avatar answered Nov 14 '22 23:11

ManojKiran Appathurai