laravel 4.2中的登录功能基于数据库记录

时间:2015-10-09 09:52:44

标签: php laravel laravel-4

我对laravel相当新,我正在尝试在laravel 4.2中创建一个登录功能,其中从数据库中获取用户名和密码。我打算在控制器中使用这个代码,但我不知道如何调整它以使用户名和密码应该来自数据库记录

chisq.test(u,v)

有什么想法吗?感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:1)

您无需调整该代码。 Auth的默认行为是使用eloquent驱动程序,该驱动程序使用您在应用程序中配置的数据库。

因此Auth::attempt($credentials)将使用与关联的数据库表(默认users表)来使用提供的凭据对用户进行身份验证。

您可以在Auth.php目录中的config文件中更改模型或表格名称,例如opitons。

修改

要验证并手动登录用户,请使用以下内容。

public function postLogin()
{
    $credentials = Input::only('username', 'password');

    $validator = Validator::make($credentials, [
        'username' => 'required',
        'password'=>'required'
    ]);

    if($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $user = User::where('SystemUserName', $credentials['username'])->first();

    if (! $user || ! Hash::check($credentials['password'], $user->SystemUserPassword)) {
        return Redirect::back()->withInput()->with('failure','username or password is invalid!');
    }

    Auth::login($user);
    return Redirect::to('user/home');
}