Laravel 在维护模式下显示自定义消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39421944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel display a custom message in Maintenance Mode
提问by raphadko
I'm checking out Laravel docs for Maintenance Mode:
我正在查看维护模式的 Laravel 文档:
https://laravel.com/docs/5.3/configuration#maintenance-mode
https://laravel.com/docs/5.3/configuration#maintenance-mode
When you execute the command php artisan down
, it will put the application under maintenance mode, and return the 503.blade.php view.
当您执行该命令时php artisan down
,它会将应用程序置于维护模式,并返回 503.blade.php 视图。
Works good, but there is an option I can't really make work.. when I do:
效果很好,但有一个选项我真的无法工作..当我这样做时:
php artisan down --message='Upgrading Database' --retry=60
I want to display the message in the view, I tried accessing the obvious choice with {{ $message }}
without success, returns undefined variable.
我想在视图中显示消息,我尝试访问明显的选择{{ $message }}
但没有成功,返回未定义的变量。
My question is: how to access it?
我的问题是:如何访问它?
回答by Antony Sklyar
Actually you don't need that "json_decode" stuff, as all the "error" views (including 503.blade.php
) have $exception
variable.
实际上你不需要那个“json_decode”的东西,因为所有的“错误”视图(包括503.blade.php
)都有$exception
变量。
So you may just use {{ $exception->getMessage() }}
in your view and you will get the exact value that you have passed to artisan down --message
command.
所以你可以只{{ $exception->getMessage() }}
在你的视图中使用,你会得到你传递给artisan down --message
命令的确切值。
回答by gmsantos
By default 503.blade.phpview doesn't use this message.
默认情况下503.blade.php视图不使用此消息。
This message is available in a JSON formatted file named storage/framework/down
generated by php artisan down
command.
此消息在名为storage/framework/down
generate byphp artisan down
command的 JSON 格式文件中可用。
You could do something like this to access the directly the message in your view:
您可以执行以下操作来直接访问视图中的消息:
{{ json_decode(file_get_contents(storage_path('framework/down')), true)['message'] }}
A cleaner way is to use the $exception
variable and include in your view {{ $exception->getMessage() }}
like suggested in this answer.
更简洁的方法是使用$exception
变量并将其包含在您的视图中,{{ $exception->getMessage() }}
如本答案中建议的那样。
Under the hood, the CheckForMaintanceMode
middleware reads the message and other data from the fileand thrown a MaintanceModeException
with this data.
在幕后,CheckForMaintanceMode
中间件从文件中读取消息和其他数据,并MaintanceModeException
用这些数据抛出一个。
回答by Taras
If you want detailed information (not just message) on your maintenance page, you can also use $exception->retryAfter
(Int), $e->willBeAvailableAt
(Carbon) and $e->wentDownAt
(Carbon).
Of course you need to set --retryparameter in artisan command.
如果您需要维护页面上的详细信息(不仅仅是消息),您还可以使用$exception->retryAfter
(Int)、$e->willBeAvailableAt
(Carbon) 和$e->wentDownAt
(Carbon)。当然你需要在 artisan 命令中设置--retry参数。