Laravel 5.7:用于维护模式的自定义刀片模板,但不是503.blade.php [英] Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php

查看:344
本文介绍了Laravel 5.7:用于维护模式的自定义刀片模板,但不是503.blade.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次打开php artisan down时,Laravel都会显示503页.

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

好.我可以通过在resources/views/errors内创建名为503.blade.php的新文件来对其进行自定义.

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

但是,尽管它使客户端无法访问网站并且对HTTP 503的描述一致,但我在任何时候都不会将维护模式视为错误.

However I don't consider Maintenance Mode as error at any point despite of the fact that it makes website unavailable for a client as well as despite of consistent description of HTTP 503:

"503服务不可用"错误是服务器端错误,这意味着 问题通常出在网站的服务器上. ...即使503 服务不可用错误表示另一个错误 计算机,这个问题可能只是暂时的.

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.

如何定义自己的刀片模板(例如maintenance_mode.blade.php)以自定义用户在应用关闭期间看到的内容,并保持503完整?

How can I define my own blade template (let's say maintenance_mode.blade.php) to customise 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?

谢谢

推荐答案

一种方法可能是在Exception的Handler中更改渲染方法.像这样:

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');
    }

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

这篇关于Laravel 5.7:用于维护模式的自定义刀片模板,但不是503.blade.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆