如何在Laravel 5.2中使用不同的数据库表列名登录? [英] How to get login with different database table column name in Laravel 5.2?
问题描述
我必须在Laravel 5.2中实现登录功能.我已经使用Laravel官方文档成功地做到了这一点,除了我不知道如何使用不同的数据库表列名称(即st_username
和st_password
)对用户进行身份验证.
I have to implement login functionality in Laravel 5.2. I have successfully done so using the official Laravel documentation except that I do not know how to authenticate the user using different database table column names, namely st_username
and st_password
.
我在互联网上搜索了线索,但无济于事. 我不知道我需要使用哪个类(例如,使用Illuminate ....)进行Auth.如果有人知道答案,请告诉我.
I have searched the Internet for clues but to no avail. I don't know which class I need to use (like, use Illuminate.......) for Auth. If any one knows the answer, please let me know.
这是我的代码:
登录视图
@extends('layouts.app')
@section('content')
<div class="contact-bg2">
<div class="container">
<div class="booking">
<h3>Login</h3>
<div class="col-md-4 booking-form" style="margin: 0 33%;">
<form method="post" action="{{ url('/login') }}">
{!! csrf_field() !!}
<h5>USERNAME</h5>
<input type="text" name="username" value="abcuser">
<h5>PASSWORD</h5>
<input type="password" name="password" value="abcpass">
<input type="submit" value="Login">
<input type="reset" value="Reset">
</form>
</div>
</div>
</div>
</div>
<div></div>
@endsection
AuthController
AuthController
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
$this->username = 'st_username';
$this->password = 'st_password';
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
路由文件
Route::get('/', function () {
return view('index');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController@index');
});
config/auth.php
config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
推荐答案
我大量研究了如何自定义 Laravel 5.2 授权表单,这对我100%有用. 这是从底部到顶部的解决方案.
I searched a lot how to customize Laravel 5.2 authorisation form and this is what is working for me 100%. Here is from bottom to top solution.
此解决方案最初来自这里: https://laracasts.com/discuss/channels/laravel/replacing-the-laravel-authentication-with-a-custom-authentication
This solution is originally from here: https://laracasts.com/discuss/channels/laravel/replacing-the-laravel-authentication-with-a-custom-authentication
但我必须进行一些更改才能使其正常工作.
but i had to make couple changes to make it work.
我的Web应用程序用于DJ,因此我的自定义列名称使用'dj_',例如,名称是dj_name
My web app is for the DJs so my custom column names are with 'dj_', for example name is dj_name
-
config/auth.php
config/auth.php
// change this
'driver' => 'eloquent',
// to this
'driver' => 'custom',
config/app.php中的
将您的自定义提供程序添加到列表中 ...
in config/app.php add your custom provider to the list ...
'providers' => [
...
// add this on bottom of other providers
App\Providers\CustomAuthProvider::class,
...
],
在文件夹app \ Providers中创建CustomAuthProvider.php
Create CustomAuthProvider.php inside folder app\Providers
namespace App\Providers;
use Illuminate\Support\Facades\Auth;
use App\Providers\CustomUserProvider;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Auth::provider('custom', function($app, array $config) {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
return new CustomUserProvider($app['custom.connection']);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
还在文件夹app \ Providers中创建CustomUserProvider.php
Create CustomUserProvider.php also inside folder app\Providers
<?php
namespace App\Providers;
use App\User; use Carbon\Carbon;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
class CustomUserProvider implements UserProvider {
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier)
{
// TODO: Implement retrieveById() method.
$qry = User::where('dj_id','=',$identifier);
if($qry->count() >0)
{
$user = $qry->select('dj_id', 'dj_name', 'first_name', 'last_name', 'email', 'password')->first();
$attributes = array(
'id' => $user->dj_id,
'dj_name' => $user->dj_name,
'password' => $user->password,
'email' => $user->email,
'name' => $user->first_name . ' ' . $user->last_name,
);
return $user;
}
return null;
}
/**
* Retrieve a user by by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token)
{
// TODO: Implement retrieveByToken() method.
$qry = User::where('dj_id','=',$identifier)->where('remember_token','=',$token);
if($qry->count() >0)
{
$user = $qry->select('dj_id', 'dj_name', 'first_name', 'last_name', 'email', 'password')->first();
$attributes = array(
'id' => $user->dj_id,
'dj_name' => $user->dj_name,
'password' => $user->password,
'email' => $user->email,
'name' => $user->first_name . ' ' . $user->last_name,
);
return $user;
}
return null;
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token)
{
// TODO: Implement updateRememberToken() method.
$user->setRememberToken($token);
$user->save();
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
// TODO: Implement retrieveByCredentials() method.
$qry = User::where('email','=',$credentials['email']);
if($qry->count() > 0)
{
$user = $qry->select('dj_id','dj_name','email','password')->first();
return $user;
}
return null;
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials)
{
// TODO: Implement validateCredentials() method.
// we'll assume if a user was retrieved, it's good
// DIFFERENT THAN ORIGINAL ANSWER
if($user->email == $credentials['email'] && Hash::check($credentials['password'], $user->getAuthPassword()))//$user->getAuthPassword() == md5($credentials['password'].\Config::get('constants.SALT')))
{
//$user->last_login_time = Carbon::now();
$user->save();
return true;
}
return false;
}
}
更改所有 将名称"更改为"dj_name",并添加自定义字段(如果有)...您还可以将电子邮件"更改为电子邮件列名称
in App/Http/Controllers/Auth/AuthController.php change all 'name' to 'dj_name' and add your custom fields if you have them...you can also change 'email' to your email column name
在Illuminate \ Foundation \ Auth \ User.php中添加
In Illuminate\Foundation\Auth\User.php add
protected $table = 'djs';
protected $primaryKey = 'dj_id';
在App/User.php中,将名称"更改为"dj_name",然后添加您的自定义字段. 要将密码"列更改为自定义列名称,请添加
In App/User.php change 'name' to 'dj_name' and add your custom fields. For changing 'password' column to your custom column name add
public function getAuthPassword(){
return $this->custom_password_column_name;
}
现在后端已全部完成,因此您只需要更改布局login.blade.php,register.blade.php,app.blade.php ...在这里,您只需要将'name'更改为' dj_name",电子邮件或您的自定义字段... !!!密码字段需要保持命名密码!!!
Now backend is all done, so you only have to change layouts login.blade.php, register.blade.php, app.blade.php...here you only have to change 'name' to 'dj_name', email, or your custom fields... !!! password field NEEDS to stay named password !!!
此外,要进行唯一的电子邮件验证更改,请修改AuthController.php
Also, to make unique email validation change AuthController.php
'custom_email_field' => 'required|email|max:255|unique:users',
to
'custom_email_field' => 'required|email|max:255|unique:custom_table_name',
此外,如果您要自定义created_at为Updated_at字段,请更改Illuminate \ Database \ Eloquent \ Model.php中的全局变量
Also, if you want to make custom created_at an updated_at fields change global variables in Illuminate\Database\Eloquent\Model.php
const CREATED_AT = 'dj_created_at';
const UPDATED_AT = 'dj_updated_at';
这篇关于如何在Laravel 5.2中使用不同的数据库表列名登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!