sarav/laravel-multiauth

一个用于处理多重认证的简单Laravel包

0.0.7 2015-12-24 09:22 UTC

This package is not auto-updated.

Last update: 2024-09-26 15:30:27 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

一个用于处理多重认证的简单Laravel包

在此处查看此项目的示例代码 here

##步骤 1 : 需求Composer包##

打开您的终端并导航到您的laravel文件夹。现在运行以下命令

composer require sarav/laravel-multiauth

或者

"require": {
    "sarav/laravel-multiauth": "^0.0.7"
}

##步骤 2 : 替换默认的认证服务提供者##

将 "Illuminate\Auth\AuthServiceProvider::class" 替换为 "Sarav\Multiauth\MultiauthServiceProvider::class"

##步骤 3 : 修改 auth.php##

修改配置目录中的 auth.php 文件,使其类似如下

'multi' => [
    'user' => [
        'driver' => 'eloquent',
        'model'  => App\User::class,
        'table'  => 'users'
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model'  => App\Admin::class,
        'table'  => 'admins'
    ]
 ],

注意:我在这里将第二个用户设为管理员。您可以随意更改,但不要忘记添加相应的驱动、模型和表。

完成!现在您可以使用以下代码简单地登录用户/管理员。

\Auth::loginUsingId("user", 1); // Login user with id 1

\Auth::loginUsingId("admin", 1); // Login user with id 1

// Attempts to login user with email id johndoe@gmail.com 
\Auth::attempt("user", ['email' => 'johndoe@gmail.com', 'password' => 'password']);

// Attempts to login admin with email id johndoe@gmail.com
\Auth::attempt("admin", ['email' => 'johndoe@gmail.com', 'password' => 'password']); 

只需将第一个参数作为在 auth.php 中配置的键传递,即可执行用户或管理员的身份验证。

##改进的Guard类##

现在您可以通过“with”函数轻松地传递Guard类。

$auth = $auth->with('admin');

更多详细信息,请参阅 这篇文章