Laravel 5如何在具有不同表的两个不同登录表单上使用身份验证 [英] Laravel 5 How to work with authentication on two different login forms with different tables

查看:65
本文介绍了Laravel 5如何在具有不同表的两个不同登录表单上使用身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用两种不同的注册表格和两种不同的表格创建了两种不同的登录形式,目前我可以执行以下操作

I created two different login forms with two different register forms and two different tables ,at the moment i can do the following

Login into table A(users)
Register into table A(users)
Register into table B(students)

但是我无法在表B上登录,就像它在要登录的表上感到困惑一样.我只是修改了auth内置功能

But i can't logon on table B ,is like it is getting confused on which table to logon .I just modified the auth built-in functionality

这是我用于登录的代码功能

Here is my code function for login under

     public function postLoginl(Request $request)
{
    $this->validate($request, [
        'learnerCell'=> 'required', 'password' => 'required',
    ]);

    $credentials = $this->getCredentialsl($request);

    if (Auth::attempt($credentials, $request->has('remember'))) {
        return redirect()->intended($this->redirectPath());
    }

    return redirect($this->loginPath())
        ->withInput($request->only('learnerCell', 'remember'))
        ->withErrors([
            'learnerCell' => $this->getFailedLoginMessage(),
        ]);
}

当我检查config/auth.php时,有一个脚本

When I check on config/auth.php there is a script

 <?php

  return [
     'driver' => 'eloquent',
      'model' => App\User::class,
      'table' => 'users',
           'password' => [
    'email' => 'emails.password',
    'table' => 'password_resets',
    'expire' => 60,
  ],

];

我认为这是问题所在,因为它没有模型来控制登录,因此它仅引用一个模型(用户),而我引用了另一个模型(学习者).

of which i think is where the problem lies,because it does not have model to control the login it only references one model (User) and I have another one called (Learner).

推荐答案

可以按以下步骤完成

app\Http\Controllers\Student下,如下创建AuthCotroller.该控制器将处理学生身份验证.

Under app\Http\Controllers\Student create AuthCotroller as below. This controller will handle student authentication.

<?php namespace App\Http\Controllers\Student;

use App\Http\Requests;
use App\Http\Controllers\Auth\AuthController as MainAuthController;

use Illuminate\Http\Request;

class AuthController extends MainAuthController {

    public $loginPath = '/student/login';

    public $redirectPath = '/student/dashboard';

    public $redirectAfterLogout = '/student/login';

    /**
     * Show the application login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function getLogin()
    {
        return view('student.login');
    }

    /**
     * Log the user out of the application.
     *
     * @return \Illuminate\Http\Response
     */
    public function getLogout()
    {
        $this->auth->logout();

        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
    }

    /** This method overrides Trait method. So, We can redirect Different Users to different destinations
     * Get the post register / login redirect path.
     *
     * @return string
     */
    public function redirectPath()
    {
        if (property_exists($this, 'redirectPath'))
        {
            return $this->redirectPath;
        }
        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/student/dashboard';
    }
}

现在创建以下中间件,

<?php namespace App\Http\Middleware;

use Closure;

class ChangeUserToStudent
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        \Config::set('auth.table', 'students');
        \Config::set('auth.model', 'App\DB\Student');

        \Config::set('session.cookie', 'student_session');
        \Config::set('session.path', '/student/');

        return $next($request);
    }

}

现在app/Http/kernel.php在中间件上方向其他中间件进行注册,如下所示,

Now in app/Http/kernel.php register above middleware with other middleware as below,

/**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
          .....
    'user.student' => 'App\Http\Middleware\ChangeUserToStudent',
          .....
        ];

现在在routes.php中,使用中间件user.student创建以下路由组,

now in routes.php create following routes group with middleware user.student,

<?php
// Protected Routes by auth and acl middleware
Route::group(['prefix' => 'student', 'namespace' => 'Student', 'middleware' => ['user.student']], function () {
    Route::get('login', [
        'as' => 'student.login',
        'uses' => 'AuthController@getLogin'
    ]);
    Route::get('logout', [
        'as' => 'student.logout',
        'uses' => 'AuthController@getLogout'
    ]);
    Route::post('login', 'AuthController@postLogin');
});
//Other student routes accessed only after login
Route::group(['prefix' => 'student', 'namespace' => 'Student', 'middleware' => ['user.student','auth']], function () {

    Route::get('dashboard', [
        'as'         => 'student.dashboard',
        'uses'       => 'DashboardController@index'
    ]);
]);

我希望您已经创建了Student模型和students表,它们基本上应该与users表相同. 现在,当您使用student前缀访问路由时,user.student中间件将跳入并将身份验证表更改为students,并即时将模型更改为Student.正如我已经显示的,您甚至可以为student设置不同的会话.

I hope you have already created Student model and students table which should essentially be at least same as a users table. Now, while you access route with student prefix, user.student middleware will jumps in and changes the authentication table to students and model to Student on the fly. You can even have different session for student as I have already shown.

view目录下,您可以将所有与学生相关的视图放在student目录下.我假设您有单独的学生登录表格 您应该在view\student目录下创建.

Under view directory you can put all student related views under student directory. I assume you have separate login form for student which you should create under view\student directory.

这样,您可以将student部分与users部分完全分开. 希望对您有所帮助.

This way you can completely separate student section from users section. I hope it helps.

这篇关于Laravel 5如何在具有不同表的两个不同登录表单上使用身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆