Laravel-artisan停机/维护模式(自有IP除外)
目前我正在使用Laravel5。 我的问题是,如果我将维护模式与Laravel-artisan停机/维护模式(自有IP除外),laravel,Laravel,目前我正在使用Laravel5。 我的问题是,如果我将维护模式与 php artisan down 怎么能说“除了我自己的ip之外,所有人的应用程序都停止了”? 所以每个人都看到了维护模式,但我仍然可以访问该站点。在Laravel 5中,您必须创建自己的中间件。 在app/Http/Middleware/checkformainancemode.php中创建一个文件 当然,您可以选择任何文件名 <?php namespace App\Http\Middleware; use Closu
php artisan down
怎么能说“除了我自己的ip之外,所有人的应用程序都停止了”?
所以每个人都看到了维护模式,但我仍然可以访问该站点。在Laravel 5中,您必须创建自己的中间件。 在app/Http/Middleware/checkformainancemode.php中创建一个文件 当然,您可以选择任何文件名
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as MaintenanceMode;
class CheckForMaintenanceMode {
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function handle(Request $request, Closure $next)
{
if ($this->app->isDownForMaintenance() &&
!in_array($request->getClientIp(), ['8.8.8.8', '8.8.4.4']))
{
$maintenanceMode = new MaintenanceMode($this->app);
return $maintenanceMode->handle($request, $next);
}
return $next($request);
}
}
到
创建新的中间件
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
class CheckForMaintenanceMode
{
protected $request;
protected $app;
public function __construct(Application $app, Request $request)
{
$this->app = $app;
$this->request = $request;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->app->isDownForMaintenance() &&
!in_array($this->request->getClientIp(), ['::1']))
{
throw new HttpException(503);
}
return $next($request);
}
}
解决问题的另一个方法是在本地开发,这样只有您才能看到应用程序的开发版本。每个人都可以看到的应用程序,即生产服务器上的应用程序,可以设置为维护模式,直到您准备向全世界展示它为止。然后可以将应用程序部署到服务器并关闭维护模式
我想说的是,可能不需要自动化这个过程,因为在部署应用程序时,您可能会出现在服务器上,所以您可以手动打开和关闭该模式
使用WAMP/MAMP或专家可以非常轻松地开发本地应用程序。当然,您可以使用任何。现在您可以使用php artisan down--allow=127.0.0.1
,或者当然可以将IP更改为其他内容。还支持多个IP,也支持网络
不确定这是什么时候实现的,但在5.6中对我来说运行良好--allow=127.0.0.1
对于Laravel 8:
即使在维护模式下,您也可以使用secret选项
指定维护模式旁路令牌:
php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"
将应用程序置于维护模式后,您可以导航到
与此令牌和Laravel匹配的应用程序URL将发出
维护模式将cookie绕过到浏览器:
访问此隐藏路由时,您将被重定向到/
申请途径。一旦cookie已发布到您的
浏览器,您将能够正常浏览应用程序,就像它
未处于维护模式
在Laravel 8的维护模式中有一些新的nice功能 在index.php文件中更改地址maintenace.php 例如: }
我使用的是laravel 8。这不是内置功能。你必须自己做,或者搜索第三方解决方案。被@andonovn的答案取代,重新发布文档链接。它曾经被其他人作为评论发布在这里,但它似乎因为某种原因被删除了。
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
class CheckForMaintenanceMode
{
protected $request;
protected $app;
public function __construct(Application $app, Request $request)
{
$this->app = $app;
$this->request = $request;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->app->isDownForMaintenance() &&
!in_array($this->request->getClientIp(), ['::1']))
{
throw new HttpException(503);
}
return $next($request);
}
}
php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"
if (file_exists(__DIR__.'/../your-file-name/storage/framework/maintenance.php')) {
require __DIR__.'/../your-file-name/storage/framework/maintenance.php';