Php Laravel 5如何在具有不同表的两个不同登录表单上使用身份验证

Php Laravel 5如何在具有不同表的两个不同登录表单上使用身份验证,php,laravel,laravel-5,Php,Laravel,Laravel 5,我用两个不同的注册表和两个不同的表创建了两个不同的登录表单,目前我可以执行以下操作 Login into table A(users) Register into table A(users) Register into table B(students) 但是我不能登录表B,就像它在哪个表上登录一样。我只是修改了auth内置功能 下面是我的登录代码函数 public function postLoginl(Request $request) { $this->vali

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

Login into table A(users)
Register into table A(users)
Register into table B(students)
但是我不能登录表B,就像它在哪个表上登录一样。我只是修改了auth内置功能

下面是我的登录代码函数

     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时,有一个脚本

 <?php

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

];

我假设您使用的是Laravel 5.1-如果不是这样,请告诉我,我也会尝试帮助您使用其他版本

最简单的方法是使用附加的类型标志将用户存储在单个表中。我知道您希望有2个不同的登录过程和用于登录的不同凭据。在同一个表中有用户后,方法是:

class UserController {
    use AuthenticatesUsers;

    //use email for authentication
    public $username = 'email';

    protected function getCredentials(Request $request)
    {
        //allow only users with type=user
        return array_merge($request->only('email', 'password'), ['type' => 'user']);
    }
}

class LearnerController {
    use AuthenticatesUsers;

    //use phone for authentication
    public $username = 'phone';

    protected function getCredentials(Request $request)
    {
        //allow only users with type=learner
        return array_merge($request->only('phone', 'password'), ['type' => 'learner']);
    }
}

可以按如下方式进行:

/**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
          .....
    'user.student' => 'App\Http\Middleware\ChangeUserToStudent',
          .....
        ];
app\Http\Controllers\Student
create
AuthCotroller
下,如下所示。此控制器将处理学生身份验证

<?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';
    }
}
现在,在
routes.php
中,使用中间件
user.student
创建以下routes组

<?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'
    ]);
]);

是的,我在5.1上,但是它附带了大多数函数,在authenticateUsers.php下有一些getCredential函数,所以它现在如何工作?我希望我可以与你分享我的项目,看看我的意思getCredentials是authenticateUsers trait用来从数据库中获取用户的函数。因此,如果函数返回['email'=>'email@email.com“,”password“=>”xyz“,”type“=>”user']然后trait将加载类型为“user”的用户,并发送等于“user”的电子邮件email@email.com“然后将根据数据库中存储的用户密码哈希值验证提供的密码。我现在迷路了,我可以与你共享我的项目吗?如果可能的话,只是为了看看我做了什么?当然,只要给我链接,我正试图按照你的代码,但我知道运气你也想查看我的代码吗?我已经这样做了两天了我不知道我做错了我在VerifyCsrfToken.php第53行中得到了令牌不匹配异常:现在尝试登录用户student时,您可以清除浏览器缓存或从其他浏览器中尝试吗?如果不起作用,请尝试从中间件中注释session.cookie和session.path。@pinkalvansia我尝试此解决方案,但我将重定向到/auth/login而不是/panel/login(在我的情况下)
<?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'
    ]);
]);