gpt4 book ai didi

php - laravel 中的 Auth 类如何知道要与哪个表比较数据?以及要检查哪些字段?

转载 作者:行者123 更新时间:2023-12-03 23:05:22 25 4
gpt4 key购买 nike

我想在 Laravel 中实现我自己的身份验证系统,而不是使用默认的,所以我正在经历 THIS教程。它使用 laravel-4,因此显然某些代码会发生变化。下面是对用户进行身份验证的代码片段,请参见下文:

// 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)) {

Auth 类如何知道要查找哪个表并比较数据?另外它如何知道要与哪些字段进行比较?这有点令人困惑。有人可以解释一下吗?

INTRO 的 auth 类对我理解 Auth 类的工作原理非常有帮助,但我仍然没有完全得到上述问题的答案。

最佳答案

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';
}

关于php - laravel 中的 Auth 类如何知道要与哪个表比较数据?以及要检查哪些字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com