Laravel 5.5中的多个Auth

时间:2018-01-15 16:32:36

标签: php mysql laravel laravel-5 laravel-5.3

我想在身份验证中集成多个表,我已经完成了以下步骤:

首先我创建了一个迁移:

php artisan make:migration createTouristsTable

在迁移文件中:

public function up()
{
    Schema::create('tourists', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();            
        $table->timestamps();
    });
}

运行迁移

php artisan migrate

游客模特:

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Tourists extends Authenticatable
{
    use Notifiable;

    protected $table = 'tourists';

    protected $fillable = ['email',  'password'];

    protected $hidden = ['password',  'remember_token'];
}

我修改了config / auth.php文件以添加我的自定义后卫"游客"。

'guards' => [

    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'tourists'  => [
      'driver'  => 'session',
      'provider' => 'tourists',
    ],

],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'tourists' => [
        'driver' => 'eloquent',
        'model'  => App\Tourists::class,
    ],
]

现在在我的LoginController中,我需要指定我想要使用哪个后卫:

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    protected $redirectTo = '/home';

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

    public function guard()
    {
        return Auth::guard('tourists');
    }
}

现在,我如何为此表创建登录和注册表单?

1 个答案:

答案 0 :(得分:0)

您可以像创建任何其他页面一样创建新的登录和注册页面。重要的区别在于每个登录/注册页面都有单独的路由,或者根据您定义的某些条件有条件地呈现包含表单的部分,以便访客登录访客,常规用户进入常规登录。

在我看来,第三种选择最适合这种情况,就是使用组件和插槽。

https://laravel.com/docs/5.5/blade#components-and-slots

问题是,您将如何确定未经身份验证的用户应该去哪个页面?