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

2

5

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

Bunko answered 4/1, 2019 at 4:28 Comment(1)
i have edited the answer check it nowVerboten
S
8

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

Sabrasabre answered 4/1, 2019 at 9:11 Comment(2)
Apparently modifying render inside Handler.php was the easiest solution. Thanks for the hint.Bunko
Be careful, MaintenanceModeException is now deprecated and may break anytime in future versions of laravel. I wonder if we can just check for $exception->getStatusCode() == 503 for detecting the good HttpException now.Mould
V
3

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

Verboten answered 4/1, 2019 at 5:31 Comment(3)
Yes, it's irrelevant. I want a custom blade template rather than custom message. I know I can change it thanks to the docs. Also I see no point of using 3rd party package just to use different template. I keep my software clean and minimal. Thanks for your efforts anyway.Bunko
Tried with laravel 8, down --message is not usable anymore.Mould
It's because in laravel 8 . Maintenance mode is changed entirelyVerboten

© 2022 - 2024 — McMap. All rights reserved.