我有一个现有的项目在php。我正在移动项目从核心到laravel。但我得到的问题,从现有的表实现管理认证。我对此做了很多研究,发现有一些命令可以自动创建身份验证功能。
请有人帮助我手动创建认证过程使用laravel模型和从存在的表表
我认为你可以这样做:
-
通过更改或添加这些列来修改您现有的用于Laravel身份验证的表:
-
username
(string) -
password
(string) -
created_at
(datetime) -
updated_at
(datetime)
remember_token
(string) -
-
将现有密码更改为Bcrypt哈希类型,这是Laravel默认的哈希方法,或者阅读类似的指南来强制Laravel在您现有的哈希函数中工作
-
制作Laravel
User
模型。您可以像使用Laravel Migration的任何教程一样进行操作。它必须是这样的:<?php namespace App; use Illuminate'Auth'Authenticatable; use Illuminate'Database'Eloquent'Model; use Illuminate'Auth'Passwords'CanResetPassword; use Illuminate'Contracts'Auth'Authenticatable as AuthenticatableContract; use Illuminate'Contracts'Auth'CanResetPassword as CanResetPasswordContract; class User extends Model implements AuthenticatableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword; protected $table = 'users'; protected $fillable = ['password', 'username', 'email']; protected $hidden = ['password', 'remember_token']; }
-
阅读Laravel认证类指南。这很简单,例如,您可以使用
Auth::check()
功能检查用户是否登录,或者尝试像这样登录:if (Auth::attempt(['email' => $email, 'password' => $password])) { // Authentication passed... }