dtwr2012 2017-04-07 17:13
浏览 54
已采纳

登录后才启用注册页面 - Laravel 5.4

I'm using make:auth method in laravel 5.4. Now, I want to change the behavior of the Register option. I want to have the register option only for users who logged in as Admin and for other users the register option should be disabled. The original version allows you to register before being authenticated. I want to use this register option to make admin to add new users. I tried option of authenticating user and redirect to register page in home.blade.php but it doesn't work. I am learning laravel for my new project.So any expert advice on how to proceed.

  • 写回答

2条回答 默认 最新

  • dongmei9508 2017-04-07 17:23
    关注

    Currently there's a method like this in App\Http\Controllers\Auth\RegisterController

    public function __construct()
    {
        $this->middleware('guest');
    }
    

    This is currently making it so that only users who are not logged in can access the page. Instead, let's change that to meet the needs of one of your criteria:

    public function __construct()
    {
        $this->middleware('auth');
    }
    

    Great, now they must be logged in to access it. But what about being an Admin? Let's make that Middleware, now:

    php artisan make:middelware AdminMiddleware
    

    Now let's open the file at App\Http\Middleware\AdminMiddleware and make some adjustments.

    Note I have to make some assumptions about your codebase here.

    public function handle($request, Closure $next)
    {
        if ( auth()->check() && auth()->user()->hasRole('admin')) {
            return $next($request);
        }
    
        return redirect('/');
    }
    

    Next, let's register our application middleware. Open up App\Http\Kernel.php and scroll to the bottom:

    protected $routeMiddleware = [
        'admin' => \App\Http\Middleware\AdminMiddleware::class, // <--add this
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
    

    Finally let's go back to App\Http\Controllers\Auth\RegisterController and update our construct function to utilize this middleware:

    public function __construct()
    {
        $this->middleware(['auth', 'admin']);
    }
    

    Now your user must be logged in, and they also must have a role of admin.

    There's surely shorter ways to get this done, but this is a method that I prefer as it will help you from repeating yourself (DRY programming).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题