您应该使用artisian命令php artisan make:auth。这将创建一些内容。它会在你的Controllers文件夹和View文件夹下创建一个auth文件夹。假设您正在使用Laravel 5.4,它还会添加到您的web.php路由文件中。在controller/auth目录中,您将找到LoginController和RegisterController,这应该已经包含了您需要进行身份验证的所有内容。您需要确保User模型上有一个电子邮件或用户名属性,并且您需要一个密码属性。从那里开始,根据您的应用程序可能需要进行一些自定义。以下是示例LoginController:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/customer';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected $username = 'email';
public function loginNameOrEmail(Request $request)
{
$field = filter_var($request->input('email'), FILTER_VALIDATE_EMAIL) ? 'email_address' : 'username';
$request->merge([$field => $request->input('email')]);
$this->username = $field;
return $this->login($request);
}
public function username()
{
return $this->username;
}
}
注册控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Email;
use App\PersonName;
use App\Location;
use App\Contact;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'first-name' => 'required|string|max:255',
'middle-name' => 'required|string|max:255',
'last-name' => 'required|string|max:255',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'state' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'password' => 'required|string|min:4|confirmed',
]);
}
protected function create(array $data)
{
$email_address = $data['email'];
$email = new Email();
$email->setEmail($email_address);
$location = new Location([
'address' => $data['address'],
'city' => $data['city'],
'state' => $data['state'],
'zipcode' => $data['zip']
]);
$location->save();
$location->createCoordinates();
$personname = new PersonName([
'first_name' => $data['first-name'],
'last_name' => $data['middle-name'],
'middle_name' => $data['last-name'],
'preferred_name' => $data['preferred-name'],
'title' => $data['title']
]);
$personname->save();
$contact = new Contact();
$contact->email_id = $email->id;
$contact->location_id = $location->id;
$contact->personname_id = $personname->id;
$contact->save();
$user = new User();
$user->contact_id = $contact->id;
$user->email_address = $email_address;
$user->setPassword($data['password']);
$user->username = $user->getEmailUsername();
$user->save();
return $user;
}
}
routes/web.php :
<?php
Route::get('/', function(){
return view('auth.login');
});
Auth::routes();
Route::post('login', 'Auth\LoginController@loginNameOrEmail');
Route::group(['middleware' => ['auth']], function () {
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
Route::get('/customer', 'CustomerController@index');
Route::post('/customer', 'CustomerController@show');
Route::get('/titleEnum', 'EnumController@title');
Route::get('/genderEnum', 'EnumController@gender');
Route::get('/test', 'TestController@test');
});
为什么不使用用户模型:
<?php
namespace App;
use Mockery\Exception;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Model as Model;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable;
protected $fillable = [
'contact', 'username', 'email_address'
];
protected $rememberTokenName = 'remember_token';
protected $hidden = [
'remember_token', 'active'
];
protected $guarded = [
'created_at', 'updated_at', 'password_hash'
];
protected $table = "user";
public $incrementing = false;
public function contact(){
return $this->hasOne(Contact::class);
}
public function customers(){
return $this->hasMany(Customer::class);
}
public function __construct($attributes = array()) {
parent::__construct($attributes);
$this->active = 1;
return $this;
}
public function setPassword($password){
try{
$this->isActive();
$this->password_hash = Hash::make($password);
$this->save();
} catch(\Exception $e) {
dump($e->getMessage());
}
return $this;
}
public function isActive(){
if($this->active) {
return true;
} else {
Throw new Exception('This user is not active. Therefore you cannot change the password', 409);
}
}
public function getEmailUsername(){
$contact = Contact::getObjectById($this->contact_id);
$email = Email::getObjectById($contact->email_id);
return $email->username_prefix;
}
public function getFullName(){
return $this->first_name . ' ' . $this->last_name;
}
public function getAuthIdentifierName(){
return $this->getKeyName();
}
public function getAuthIdentifier(){
return $this->{$this->getAuthIdentifierName()};
}
public function getAuthPassword(){
return $this->password_hash;
}
public function getRememberToken(){
if (! empty($this->getRememberTokenName())) {
return $this->{$this->getRememberTokenName()};
}
}
public function setRememberToken($value){
if (! empty($this->getRememberTokenName())) {
$this->{$this->getRememberTokenName()} = $value;
}
}
public function getRememberTokenName(){
return $this->rememberTokenName;
}
public function getEmailForPasswordReset(){
}
public function sendPasswordResetNotification($token){
}
public function validateAddress(){
}
}
php artisan make:auth
,它可以帮助你创建身份验证脚手架。 - Nikhil Radadiyaphp artisan make:auth
,那么它应该在那里。 - Nikhil Radadiya