【问题标题】:Laravel display a custom message in Maintenance ModeLaravel 在维护模式下显示自定义消息
【发布时间】:2017-01-18 05:12:02
【问题描述】:

我正在查看 Laravel 文档以了解维护模式:

https://laravel.com/docs/5.3/configuration#maintenance-mode

当你执行命令php artisan down时,它会将应用程序置于维护模式,并返回503.blade.php视图。

效果很好,但有一个选项我无法真正发挥作用......当我这样做时:

php artisan down --message='Upgrading Database' --retry=60

我想在视图中显示消息,我尝试使用{{ $message }} 访问明显的选择但没有成功,返回未定义的变量。

我的问题是:如何访问它?

【问题讨论】:

    标签: laravel laravel-5.3


    【解决方案1】:

    默认情况下503.blade.php 视图不使用此消息。

    此消息在名为 storage/framework/down generated by php artisan down 命令的 JSON 格式文件中可用。

    您可以执行以下操作来直接访问视图中的消息:

    {{ json_decode(file_get_contents(storage_path('framework/down')), true)['message'] }}
    

    一种更简洁的方法是使用$exception 变量并在您的视图中包含{{ $exception->getMessage() }},就像this answer 中建议的那样。

    在后台,CheckForMaintanceMode 中间件读取 message and other data from the file 并使用此数据抛出 MaintanceModeException

    编辑:在 Laravel 8 之后,创建 storage/framework/down 命令的负载发生了变化,而不是 include the exception message。你应该在 Laravel 8+ 上使用{{ $exception->getMessage() }}

    【讨论】:

    • @jezmck 感谢您的链接。我觉得原版有点脏! {{ $exception->getMessage() }} 是正确获取消息的方法:)
    • Antony 的回答确实是一种更简洁的方式。我已经用一些额外的信息引用了他的答案。在引擎盖下,Laravel 仍然从​​该文件中读取 :)
    • 这对于最新的 laravel 8 不再有效
    【解决方案2】:

    如果您想要维护页面上的详细信息(不仅仅是消息),您还可以使用$exception->retryAfter(Int)、$e->willBeAvailableAt(Carbon) 和 $e->wentDownAt(Carbon)。 当然你需要在 artisan 命令中设置 --retry 参数。

    【讨论】:

      【解决方案3】:

      实际上你不需要那些“json_decode”的东西,因为所有的“错误”视图(包括503.blade.php)都有$exception变量。

      因此,您可以只在视图中使用{{ $exception->getMessage() }},您将获得传递给artisan down --message 命令的确切值。

      【讨论】:

      • 也可以使用{{ $exception->retryAfter }}来显示时间。
      猜你喜欢
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      相关资源
      最近更新 更多