简体   繁体   English

Laravel 5.2 自定义身份验证

[英]Laravel 5.2 Custom Authentication

I'm using a table named admins to store name|email|password|contact of all the admins instead of the built in users table provided by Laravel and my login/logout controller code is given below.我正在使用一个名为admins的表来存储所有管理员的name|email|password|contact ,而不是 Laravel 提供的内置用户表,我的登录/注销控制器代码如下。

It's not working perfectly and not even throwing any error.它工作不完美,甚至没有抛出任何错误。 Do I need to configure config/auth.php file for this?我需要为此配置 config/auth.php 文件吗? If I need to then what should it look like?如果我需要,那么它应该是什么样子? And suppose I have another table for managers and they have name|email|password|contact like admins.假设我有另一个经理表,他们有name|email|password|contact就像管理员一样。 How can I configure them?我该如何配置它们?

AdminloginController:管理员登录控制器:

<?php

namespace App\Http\Controllers;


use App\Model\Admin;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class AdminLoginController extends Controller
{
    public function postLogin(Request $request){
        if(Auth::attempt(['email'=> $request->email,'password'=> $request->password])){
            return redirect('/AdminPanel');
        }
        return redirect()->back();
    }
    public function getLogout(Request $request){
        Auth::logout();
        return redirect('/');
    }
}

If you have multiple tables with same columns you should rather not create many tables.如果您有多个具有相同列的表,则不应创建多个表。 It would be better if you use one table (might be default users ) and for each user set also role (either admin or manager ).如果您使用一个表(可能是默认users )并且为每个用户设置角色( adminmanager )会更好。 It might not make any point to create multiple users tables.创建多个用户表可能没有任何意义。 Consider also that in future you might need also different type of managers etc. and you will finally have a few users table instead of one.还要考虑到将来您可能还需要不同类型的管理器等,并且您最终将拥有几个用户表而不是一个。

You should really consider this when you don't have more tables yet to not have do many changes in your code in future.当您没有更多的表并且将来不会对代码进行很多更改时,您真的应该考虑这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM