2016-04-04 165 views
5

我想只允許某些IP地址來訪問該寄存器路線,但我發現了錯誤類型:只允許某些IP地址在Laravel註冊用戶5.2

Argument 1 passed to App\Http\Controllers\Auth\AuthController::showRegistrationForm() must be an instance of Illuminate\Http\Request, none given, called in directory\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 19

這裏是我的showRegistrationForm方法在AuthController

public function showRegistrationForm(Request $request) { 
    $ip = $request->ip(); 

    if ($ip == "148.124.0.1") { 
     return redirect("register"); 
    } else { 
     return redirect("home"); 
    } 
} 

有沒有人知道我該怎麼做到這一點?

+0

你明白了什麼,當你的DD($請求)? – Frisbetarian

+0

正在從您當地的環境進行測試 –

+0

@ maytham-ɯɐɥʇʎɐɯ是的。 –

回答

18

在控制器而不是檢查請這樣

php artisan make:middleware IpMiddleware 

代碼

<?php 

namespace App\Http\Middleware; 

use Closure; 

class IpMiddleware 
{ 

    public function handle($request, Closure $next) 
    { 
     if ($request->ip() != "192.168.0.155") { 
     // here insted checking single ip address we can do collection of ip 
     //address in constant file and check with in_array function 
      return redirect('home'); 
     } 

     return $next($request); 
    } 

} 

然後添加新的中間件類在app/Http/Kernel.php類的$ middleware財產。

protected $routeMiddleware = [ 
    'auth' => \App\Http\Middleware\Authenticate::class, 
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 
    'ipcheck' => \App\Http\Middleware\Ip::class, 
]; 

然後申請middelware路線

Route::get('/', ['middleware' => ['ipcheck'], function() { 
    // your routes here 
}]); 

希望此舉能幫助你

+0

謝謝@pramod kadam,我會嘗試這種方法。 –

+0

謝謝..只是爲了補充一點,如果你有像我這樣的公共存儲庫,最好使用'.env'來存儲該ip地址... –

+0

''ipcheck'=> \ App \ Http \ Middleware \ Ip :: class',應該是''ipcheck'=> \ App \ Http \ Middleware \ IpMiddleware :: class' – Paddy

相關問題