2017-01-11 22 views
1

我想在laravel中实现自己的身份验证系统,而不是默认的,所以我正在通过THIS教程。它使用laravel-4,所以显然有些代码会改变。下面有当用户进行认证的代码片段,见下图:laravel中的Auth类如何知道哪个表比较数据?还有哪些字段需要检查?

// app/controllers/HomeController.php< 


public function doLogin() { 
// validate the info, create rules for the inputs 
$rules = array(
    'email' => 'required|email', // make sure the email is an actual email 
    'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters 
); 

// run the validation rules on the inputs from the form 
$validator = Validator::make(Input::all(), $rules); 

// if the validator fails, redirect back to the form 
if ($validator->fails()) { 
    return Redirect::to('login') 
     ->withErrors($validator) // send back all errors to the login form 
     ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form 
} else { 

     // create our user data for the authentication 
     $userdata = array(
      'email'  => Input::get('email'), 
      'password' => Input::get('password') 
     ); 

     // attempt to do the login 
     if (Auth::attempt($userdata)) { 

      // validation successful! 
      // redirect them to the secure section or whatever 
      // return Redirect::to('secure'); 
      // for now we'll just echo success (even though echoing in a controller is bad) 
      echo 'SUCCESS!'; 

     } else {   

      // validation not successful, send back to form 
      return Redirect::to('login'); 

     } 
    } 
} 

我不太明白下面的一行代码:

if (Auth::attempt($userdata)) { 

怎样的验证类知道哪个表寻找和比较数据?另外它如何知道哪些字段与之比较?这有点令人困惑。有人可以解释一下吗?

INTRO auth class对我来说很了解Auth类是如何工作的,但我仍然没有完全回答上述问题。

回答

1

Laravel知道这一切,因为列名(电子邮件和密码)和表名(用户)都是hardcoded,Laravel默认使用这些名称。

0

在Laravel 5,你应该设置身份验证表在以下部分的配置/ auth.php文件:

'providers' => [ 
     'users' => [ 
      'driver' => 'eloquent', 
      'model' => App\Models\UserModel::class, 
     ], 

     // 'users' => [ 
     //  'driver' => 'database', 
     //  'table' => 'users', 
     // ], 
    ], 

正如你看到的,你可以设置数据库的表,甚至模型。

至于auth字段,默认情况下有名称,电子邮件和密码。如果你想使用自定义字段,你应该重新声明在任何类的标准方法是使用照亮\基金会\验证\ AuthenticatesUsers:

protected function credentials(Request $request) 
{ 
    return $request->only($this->username(), 'password'); 
} 

public function username() 
{ 
    return 'email'; 
} 
2

Laravel通过config/auth.php,你应该有这样的事情在Laravel 4知道它:

/* 
    |-------------------------------------------------------------------------- 
    | Authentication Model 
    |-------------------------------------------------------------------------- 
    | 
    | When using the "Eloquent" authentication driver, we need to know which 
    | Eloquent model should be used to retrieve your users. Of course, it 
    | is often just the "User" model but you may use whatever you like. 
    | 
    */ 

    'model' => 'User', 

    /* 
    |-------------------------------------------------------------------------- 
    | Authentication Table 
    |-------------------------------------------------------------------------- 
    | 
    | When using the "Database" authentication driver, we need to know which 
    | table should be used to retrieve your users. We have chosen a basic 
    | default value but you may easily change it to any table you like. 
    | 
    */ 

    'table' => 'users', 

的用户名是硬编码的,但是,如果你需要,你应该能够加入这个方法将你的用户模型来更改:

public function username() 
{ 
    return 'username'; 
} 
相关问题