教堂门户 / laravel-scheduled-maintenance
安排维护窗口,并在您的 Laravel 应用程序关闭时定制用户体验
Requires
- php: ^8.0
- calebporzio/sushi: ^2.2
- illuminate/contracts: ^8.37
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- brianium/paratest: ^6.2
- nunomaduro/collision: ^5.3
- orchestra/testbench: ^6.15
- phpunit/phpunit: ^9.3
- spatie/laravel-ray: ^1.9
- vimeo/psalm: ^4.4
README
此包允许您为 Laravel 应用程序安排维护窗口,更易于通知用户即将进行的维护,并在应用程序维护关闭期间为用户提供定制的用户体验。
它与 Laravel 的现有 down 和 up 命令类似,支持绕过模式、重定向和自定义 HTTP 状态码。
注意:此包依赖于您的数据库,如果您在维护窗口期间执行重大的数据库工作,则可能需要考虑使用 Laravel 的 down 来执行该项工作。
安装
通过 composer 安装此包
composer require churchportal/laravel-scheduled-maintenance
发布包资源(这将发布配置文件、1 个迁移和一个示例 blade 视图)
php artisan vendor:publish --provider="Churchportal\ScheduledMaintenance\ScheduledMaintenanceServiceProvider"
运行迁移
php artisan migrate
配置
<?php return [ /* |-------------------------------------------------------------------------- | Prerequisites |-------------------------------------------------------------------------- | Configure the table name and model for maintenance windows. You can use | your own model as long as it extends the default class below. */ 'table_name' => 'scheduled_maintenance', 'model' => \Churchportal\ScheduledMaintenance\Models\ScheduledMaintenanceModel::class, /* |-------------------------------------------------------------------------- | Maintenance Defaults |-------------------------------------------------------------------------- | These defaults will be used when scheduling maintenance windows or moving | your application to maintenance mode. You can customize these for each | maintenance window as needed. */ 'redirect_to' => null, 'status_code' => 503, 'bypass_secret' => null, /* |-------------------------------------------------------------------------- | Bypass Cookie Name |-------------------------------------------------------------------------- | This cookie will be created when you visit the bypass secret url. It's | recommended that you customize this cookie name to your env or app name */ 'bypass_cookie_name' => 'laravel_maintenance', /* |-------------------------------------------------------------------------- | Excluded URIs |-------------------------------------------------------------------------- | These paths will still be accessible during maintenance mode. These will | apply to any maintenance windows so use with caution! */ 'except' => [ 'status', ], /* |-------------------------------------------------------------------------- | Blade View |-------------------------------------------------------------------------- | This view will be rendered when you application is in maintenance mode. | You should use your own view, the default we've provided is just an | example of how you could implement something. */ 'view' => 'scheduled-maintenance::down', ];
全局单例 app('maintenance')
ScheduledMaintenace 类通过 maintenance 键绑定到容器。这些类方法使您能够
- 检查应用程序是否处于维护模式:
->isDown() - 进入维护模式:
->down() - 退出维护模式:
->up() - 获取当前活跃的维护窗口:
->current() - 获取下一个计划中的维护窗口:
->next() - 获取所有未来的维护窗口:
->scheduled() - 通过 id 或 uuid 查找计划中的维护窗口:
->find($id) - 通过 id 或 uuid 删除计划中的维护窗口:
->delete($id) - 检查您是否绕过了维护模式:
->inBypassMode() - 检查是否有关于即将进行的维护的通知可供用户查看:
->notice()
Artisan 命令
maintenance:schedule
此命令将指导您创建新的维护窗口。您将被提示输入有关维护何时开始、用户何时应看到有关即将进行的维护的通知等信息。
maintenance:down
此命令将立即将您的应用程序移动到维护模式! 当运行此命令时,包将要么将下一个计划中的维护窗口移动到活动状态,要么如果没有计划中的维护,则创建新记录。
创建新记录时的选项
--bypass-secret=为此维护窗口设置绕过密钥--redirect-to=配置在应用程序处于维护模式时用户的重定向
maintenance:up
此命令将使您的应用程序退出维护模式
maintenance:upcoming
此命令将以表格格式列出所有未来的维护窗口
maintenance:cancel {id}
此命令将删除计划中的维护窗口
事件
所有事件都包含一个公开的 $scheduledMaintenace 模型属性
MaintenanceScheduled
此事件在通过 maintenance:schedule 命令安排维护后触发
MaintenanceStarted
此事件在运行 app('maintenance')->down() 后触发。还有一个额外的 $wasPreviouslyScheduled 属性,如果维护未预先安排而启动,则将为 false。
MaintenanceCompleted
此事件在运行 app('maintenance')->up() 后触发
MaintenanceCancelled
此事件在运行 app('maintenance')->delete($id) 后触发
使用方法
向用户显示有关即将进行的维护的通知
使用 app('maintenance')->notice() 方法,您可以获取下一次即将进行的维护窗口的详细信息
在 blade 中
@extends('layout') @if(app('maintenance')->notice()) <p> We'll be preforming server maintenance on {{ app('maintenance')->notice()->starts_at->format('F jS, \a\t g:ia') }} </p> @endif
在 inerita 中
// In your Inerita middleware \Inertia\Inertia::share([ 'upcomingMaintenance' => app('maintenance')->notice(), ]);
<!-- In your Inertia layout --> <UpcomingMaintenance v-if="$page.props.upcomingMaintenance" />
绕过维护模式
您可以通过导航到 bypass_secret url 来绕过维护模式。在测试您的应用程序时,记住它仍然处于维护模式可能会有所帮助。以下是如何实现该通知的一些示例
在 blade 中
@extends('layout') @if(app('maintenance')->inBypassMode()) <p> Your application is currently in maintenance mode! It should last until {{ app('maintenance')->current()->ends_at }} </p> @endif
在 inerita 中
// In your Inerita middleware \Inertia\Inertia::share([ 'bypassedMaintenance' => app('maintenance')->inBypassMode(), ]);
<!-- In your Inertia layout --> <BypassedMaintenanceBanner v-if="$page.props.bypassedMaintenance" />

