咨询 / laravel-maintenance-mode
在多个服务器上程序化管理Laravel应用的模式维护。
Requires
- illuminate/support: ^5.5 | ^6.0
Requires (Dev)
- mockery/mockery: ^1.2
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.1
- stechstudio/laravel-vfs-adapter: ^3.0
README
安装
通过Composer安装
composer require konsulting/laravel-maintenance-mode
Laravel 5.5+将自动发现提供者和外观。
配置
使用以下命令发布配置文件:
php artisan vendor:publish --provider="Konsulting\Laravel\MaintenanceMode\MaintenanceModeProvider"
中间件
使用默认配置值时,文件驱动程序与Laravel现有的维护模式中间件兼容。
要使用不同的驱动程序或不同的文件路径,请将提供的CheckForMaintenanceMode
中间件添加到app/Http/Kernel.php
。
class Kernel extends HttpKernel { // ... protected $middleware = [ \Konsulting\Laravel\MaintenanceMode\Middleware\CheckForMaintenanceMode::class, // You can remove the Illuminate CheckForMaintenanceMode middleware or leave it in \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, // ... ]; }
基本用法
默认情况下,Laravel的artisan up
和artisan down
命令被覆盖,因此可以使用所选驱动程序激活和停用维护模式。如果配置中的override_illuminate_commands
设置为false
,则维护模式的命令为site:up
和site:down
。
维护模式的行为类似于Laravel的默认artisan up
/artisan down
命令。当激活时,使用所选驱动程序存储包含中断信息的有效载荷。这被中间件捕获,如果维护模式激活,则抛出Illuminate\Foundation\Http\Exceptions\MaintenanceModeException
。Laravel的异常处理程序处理返回正确的响应。
驱动程序
默认情况下包括三个驱动程序:文件、缓存和存储。文件驱动程序与Laravel内置的维护模式非常相似,因为它在本地文件系统中存储有效载荷文件。缓存和存储驱动程序在多个web服务器托管应用程序时很有用,因为它们可以轻松配置以在缓存服务器等外部位置存储有效载荷。
有关驱动程序的更多信息,请参阅配置文件。
程序化使用
实例化
可用的驱动程序有:缓存、文件和存储。更详细的信息包含在config/maintenance_mode.php
中。
$driver = new FileDriver(['file_path' => '/path/to/file']); $maintenanceMode = new MaintenanceMode($driver);
激活维护模式
使用MaintenanceMode::on()
方法激活维护模式。
当激活维护模式时,创建包含给定信息的有效载荷对象并由所选驱动程序存储。这包含有关关闭站点的信息,包括维护模式激活的时间、重试请求之间的秒数、在维护模式期间允许访问站点的任何IP地址以及与中断相关联的消息。
$message = 'Down for database upgrades.'; $allowedIps = ['192.168.10.10', '127.0.0.1']; $secondsToRetry = 60; $maintenanceMode->on($message, $allowedIps, $secondsToRetry); $maintenanceMode->isOn(); // true
检索中断信息
$retrievedPayload = $maintenanceMode->getDownInformation(); // new DownPayload([ // 'time' => Carbon::now()->getTimestamp(), // 'message' => 'Down for database upgrades.', // 'retry' => 60, // 'allowed' => ['192.168.10.10', '127.0.0.1'], // ]);
停用维护模式
使用MaintenanceMode::off()
方法停用维护模式。