php – 仅允许某些IP地址在Laravel 5.2中注册用户

前端之家收集整理的这篇文章主要介绍了php – 仅允许某些IP地址在Laravel 5.2中注册用户前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图只允许某些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");
    }
}

有谁知道我会怎么做到这一点?

而是检查控制器,请检查这种方式
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
}]);

希望它会帮助你

猜你在找的Laravel相关文章