我想在laravel中实现我自己的auth系统而不是默认设置,所以我正在浏览 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类如何知道要查找哪个表并比较数据?另外它如何知道要比较哪些字段?这有点令人困惑。有人可以解释一下吗?
auth课程的 INTRO 非常有助于我了解Auth课程的工作原理,但我仍然无法回答上述问题。< / p>
答案 0 :(得分: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';
}
答案 1 :(得分:1)
Laravel知道所有,因为列名(电子邮件和密码)和表名(用户)是hardcoded,Laravel默认使用这些名称。
答案 2 :(得分:1)
在Laravel 5中,您应该在以下部分的config / auth.php文件中设置auth表:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\UserModel::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
如您所见,您可以设置数据库的表格甚至是模型。
至于auth字段,默认情况下有姓名,电子邮件和密码。如果要使用自定义字段,则应在使用Illuminate \ Foundation \ Auth \ AuthenticatesUsers的任何类中重新声明标准方法:
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password');
}
public function username()
{
return 'email';
}