Home >php教程 >php手册 >Laravel解决多张表身份验证的问题

Laravel解决多张表身份验证的问题

WBOY
WBOYOriginal
2016-06-06 20:13:461011browse

Laravel的auth身份验证只支持一张表,如果项目中有多个角色存储在不同表中,比如users admins,就有些不足了,所以需要进行扩展,学的不深,我也无能为力,但是国外网友有解决方案,并共享在github上。 地址:https://github.com/ollieread/multiauth 按这名

Laravel的auth身份验证只支持一张表,如果项目中有多个角色存储在不同表中,比如users admins,就有些不足了,所以需要进行扩展,学的不深,我也无能为力,但是国外网友有解决方案,并共享在github上。

地址:https://github.com/ollieread/multiauth

按这名老外的说法,这个解决方案并不会替换Laravel本身的auth库,只是介于auth库和你的代码之间的一个工厂类,废话不多说,看下怎么安装使用

安装

1.先备份的你的代码

这个不多说,如果你不想在出错后悲剧的话。

2.打开根目录下的composer.json,加入你要安装的包:

"require": {
        "ollieread/multiauth": "dev-master"
}
3.更新composer
4.打开app/config/app.php 修改 AuthServiceProvider的配置
Illuminate\Auth\AuthServiceProvider
改成
Ollieread\Multiauth\MultiauthServiceProvider
使用
1.修改app/config/auth.php
return array(
    'driver' => 'eloquent',
    'model' => 'User',
    'table' => 'users',
    'reminder' => array(
        'email' => 'emails.auth.reminder',
        'table' => 'password_reminders',
        'expire' => 60,
    ),
);
替换成
return array(
    'multi' => array(
        'account' => array(
            'driver' => 'eloquent',
            'model' => 'Account'
        ),
        'user' => array(
            'driver' => 'database',
            'table' => 'users'
        )
    ),
    'reminder' => array(
        'email' => 'emails.auth.reminder',
        'table' => 'password_reminders',
        'expire' => 60,
    ),
);
2.
Auth::account()->attempt(array(
    'email'     => $attributes['email'],
    'password'  => $attributes['password'],
));
Auth::user()->attempt(array(
    'email'     => $attributes['email'],
    'password'  => $attributes['password'],
));
Auth::account()->check();
Auth::user()->check();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn