Laravel - 工匠关闭/维护模式,除了自己的 IP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30752962/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:45:30  来源:igfitidea点击:

Laravel - artisan down / Maintenance Mode except own IP

laravel

提问by user3633186

Currently i am using Laravel5. My question is if if i use the Maintenance mode with

目前我正在使用 Laravel5。我的问题是如果我使用维护模式

php artisan down

how can say "the application is down for everyone except my own ip" ? So everyone is seeing the Maintenance mode, but i have still access to the site.

怎么能说“除了我自己的 ip 之外,其他人的应用程序都已关闭”?所以每个人都看到了维护模式,但我仍然可以访问该站点。

回答by user3633186

In Laravel 5 you have to create your own middleware. Create a file in app/Http/Middleware/CheckForMaintenanceMode.php You can choose of course any filename.

在 Laravel 5 中,您必须创建自己的中间件。在 app/Http/Middleware/CheckForMaintenanceMode.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);
    }

}

In your app/Http/Kernel.php change

在您的 app/Http/Kernel.php 更改

'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode'

to

'App\Http\Middleware\CheckForMaintenanceMode'

回答by andonovn

Now you can use php artisan down --allow=127.0.0.1, or of course change the IP to something else. Multiple IPs are also supported, networks are also supported.

现在您可以使用php artisan down --allow=127.0.0.1,当然也可以将 IP 更改为其他内容。还支持多个 IP,也支持网络。

Not sure when this was implemented but is working fine for me in 5.6

不确定何时实施,但在 5.6 中对我来说效果很好

回答by Ikong

Create new middleware

创建新的中间件

<?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);

    }

}

The '::1' is your own IP assuming your in localhost, if not the specify your IP. You can excluded multiple IP in the array. check Excluding your IP Address in Maintenance Mode (php artisan down) in Laravel 5

'::1' 是你自己的 IP 假设你在本地主机,如果不是指定你的 IP。您可以排除阵列中的多个 IP。检查在 Laravel 5 中在维护模式下排除您的 IP 地址(php artisan down)

回答by Winfried

Another solution to your problem would be to develop locally, so only you can see the development version of your application. The application which is visible by everyone, the one on your production server, could be set into maintenance mode until you're ready to show it to the world. You can then deploy your application to your server and turn off the maintenance mode.

您的问题的另一种解决方案是在本地开发,因此只有您才能看到应用程序的开发版本。每个人都可以看到的应用程序,即您生产服务器上的应用程序,可以设置为维护模式,直到您准备好向全世界展示为止。然后,您可以将应用程序部署到服务器并关闭维护模式。

What I'm saying, is that automatising this process may not be needed, as you'll probably be present on your server when you're deploying your application, so you can manually turn the mode on and off.

我的意思是,可能不需要自动化这个过程,因为当您部署应用程序时,您可能会出现在您的服务器上,因此您可以手动打开和关闭该模式。

Developing local can be quite easy using WAMP/MAMP, Laravel's Homestead, Laravel Valetor for experts Laradock. Of course, you can use any.

使用 WAMP/MAMP、Laravel's HomesteadLaravel Valet或专家Laradock可以很容易地开发本地。当然,你可以使用任何。

回答by Виталий Межебицкий

$ php artisan down --allow=127.0.0.1