我试图只允许某些IP地址访问寄存器路由,但我收到类型错误:
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
这是我在AuthController中的showRegistrationForm方法:
public function showRegistrationForm(Request $request) { $ip = $request->ip(); if ($ip == "148.124.0.1") { return redirect("register"); } else { return redirect("home"); } }
有谁知道我会怎么做到这一点?
而是检查控制器,请检查这种方式
原文链接:https://www.f2er.com/laravel/138673.htmlPHP 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 }]);
希望它会帮助你