2015-04-02 63 views
1

在Laravel 4,你可以通過這樣繞過Laravel維護模式(php artisan down)的一些IP地址:如何繞過某些IP地址Laravel 5維護模式

App::down(function() 
{ 
    if (!in_array(Request::getClientIp(), ['192.168.0.1'])) 
    { 
     return Response::view('maintenance', [], 503); 
    } 
}); 

你也可以提供一個配置文件的維護。與所有的IP地址列表的PHP允許在維護模式下,當訪問你的應用程序:

<?php 

return [ 

    /* 
    |-------------------------------------------------------------------------- 
    | Allowed IP Addresses 
    |-------------------------------------------------------------------------- 
    | Include an array of IP addresses or ranges that are allowed access to the app when 
    | it is in maintenance mode. 
    | 
    | Supported formats: 

    | 
    */ 

    'allowed_ips' => [ 
     '10.0.2.2', 
     '10.2.*.*', 
     '10.0.2.3 - 10.0.2.45', 
     '10.0.3.0-10.3.3.3' 
    ], 

]; 

我的問題是,怎樣在Laravel 5實現這一目標?

回答

2

創建新的中間件

<?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','another_IP'])) 

     { 

      throw new HttpException(503); 

     } 



     return $next($request); 

    } 

} 

'::1'是你自己的IP假設你在本地主機,如果沒有指定IP。您可以排除陣列中的多個IP。檢查Excluding your IP Address in Maintenance Mode (php artisan down) in Laravel 5

+0

嗯。涼。感謝您的回答。很有幫助。 – Digitlimit 2015-08-25 10:45:27