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条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示