我正在尝试使用 Laravel 5.7 创建第一个项目。
在这个项目中,我具有三个不同的区域,具有三个不同的登录名(例如,用户登录,管理员登录,技术人员登录)。
现在,我想自定义首次登录(URL,html,输入,类,数据库表,数据库列ecc ...),但我不知道该怎么做。
首次登录的 URL 为website.com/public/users
我的表格是:
CREATE TABLE `gf_users` (
`gf_id` int(11) NOT NULL,
`gf_name` text NOT NULL,
`gf_email` varchar(200) NOT NULL,
`gf_password` varchar(300) NOT NULL,
)
我进行了很多搜索,并根据发现的信息尝试这样做:
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\GFUsers::class
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
web.php
Route::get('users', 'Auth\LoginController@showLoginFormUsers')->name('login');
Route::post('users', 'Auth\LoginController@login');
LoginController.php
public function showLoginFormUsers(){
return view('users.login');
}
public function username(){
return 'gf_email';
}
GFUsers.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class GFUsers extends Authenticatable
{
use Notifiable;
protected $table = 'gf_users';
protected $primaryKey = 'gf_id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'gf_email', 'gf_password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'gf_password'
];
public function getAuthPassword(){
return $this->gf_password;
}
}
login.blade.php
<form method="POST" action="{{ asset('/users') }}">
@csrf
<div class="form-group">
<input type="email" class="form-control" name="gf_email" id="gf_email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="password" class="form-control" name="gf_password" id="gf_password" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-primary">LOGIN</button>
</form>
如果我编译表单并单击“登录”按钮,将刷新页面,但我不知道它是否有效。
1 。程序正确吗?
2 。有什么错误吗?
3 。我忘了什么吗?
4 。我该怎么办注册?
答案 0 :(得分:1)
config / auth.php:
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'gfuser' => [
'driver' => 'session',
'provider' => 'gfuser',
],
],
'providers' => [
'gfuser' => [
'driver' => 'eloquent',
'model' => App\GFUsers::class
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
LoginController
Auth::guard('gfuser')->login($user); //array data with username and password
OR
if (Auth::guard('gfuser')->attempt(['username' => $username, 'password' => $password]) {//Redirect to dashboard}
答案 1 :(得分:0)
最好不要为用户,管理员和技术人员创建不同的表。他们都想成为用户。在一个表中,只是具有不同的属性(例如-角色,状态,is_admin,is_user ...)。
答案 2 :(得分:0)
经过长期研究,我找到了解决方案。我希望这可以帮助其他人。
我已经自定义了:id,用户名,密码和记住表中的令牌以及User.php,LoginController.php文件。
表格:
CREATE TABLE `gf_users` (
`gf_id` int(11) NOT NULL,
`gf_name` text NOT NULL,
`gf_email` varchar(200) NOT NULL,
`gf_password` varchar(300) NOT NULL,
`gf_remember_token` varchar(300) NOT NULL,
)
web.php :
// Authentication Routes
Route::get('users', 'Auth\NewLoginController@showLoginFormUsers')->name('login');
Route::post('users', 'Auth\NewLoginController@checklogin');
Route::post('logout', 'Auth\NewLoginController@logout')->name('logout');
// Registration Routes
Route::get('users/registration', 'Auth\RegisterController@showRegistrationFormUsers')->name('register');
Route::post('users/registration', 'Auth\RegisterController@register');
NewLoginController.php :
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/users/details';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginFormUsers(){
return view('users.login');
}
public function username(){
return 'gf_email';
}
function checklogin(Request $request){
$this->validate($request, [
'input-email' => 'required',
'input-password' => 'required',
]);
$user_data = array(
'gf_email' => $request->get('input-email'),
'password' => $request->get('input-password')
);
if(!Auth::attempt($user_data)){
return redirect('users');
}
if ( Auth::check() ) {
return redirect('users/details');
}
}
function logout(){
Auth::logout();
return redirect('users');
}
}
NewUser.php :
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'gf_users';
protected $primaryKey = 'gf_id';
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'gf_email', 'gf_password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'gf_password', 'gf_remember_token',
];
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword(){
return $this->gf_password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->gf_remember_token;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->gf_remember_token = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'gf_remember_token';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->gf_email;
}
}
login.blade.php
<form method="POST" action="{{ asset('/users') }}">
@csrf
<div class="form-group">
<input type="email" class="form-control" name="input-email" id="input-email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="password" class="form-control" name="input-password" id="input-password" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-primary">LOGIN</button>
</form>
config / auth.php :
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\NewUser::class
],
//'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
更改后,我执行了命令 php artisan config:cache
。