dqz7636 2016-05-28 05:52
浏览 179
已采纳

如何在laravel中指定多个经过身份验证的防护

i'm reading in Authentication section in laravel website https://laravel.com/docs/5.2/authentication

can anyone explain how I can do this , like the documentation explains , to specify separate tables for authentication ... i will quotes from laravel like below :

Accessing Specific Guard Instances

You may specify which guard instance you would like to utilize using the guard method on the Auth facade. This allows you to manage authentication for separate parts of your application using entirely separate authenticatable models or user tables.

The guard name passed to the guard method should correspond to one of the guards configured in your auth.php configuration file:

if (Auth::guard('admin')->attempt($credentials)) {
    //
}
  • 写回答

2条回答 默认 最新

  • dongshi3605 2016-05-28 06:26
    关注

    You kinda have to read the examples of adding custom guards and providers, the configuration part of it mainly. You can use the same auth 'driver', you just want to adjust what model is used by the Auth user provider.

    config/auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        // add another one
        // use the same driver, 'session', but a different user provider
        'admin' => [
             'driver' => 'session',
             'provider' => 'admins',
         ],
    ],
    
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        // add a provider using Eloquent but using a different model
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
    ]
    

    Then you should be able to specify the guard admin to Auth. As long as that Admin model implements Authenticatable and you are passing the appropriate credentials to attempt on Auth you should be good.

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

报告相同问题?