一步一步学laravel之登录验证(一)

本文探讨了Laravel框架内置的auth模块如何实现登录验证,尽管看似复杂,但实际上是为了应对各种场景,如验证额外字段或禁止登录状态。通过分析`getFacadeAccessor`和`registerAuthenticator`方法,揭示了AuthManager和guard对象的角色。接下来的文章将进一步剖析登录的具体流程。
摘要由CSDN通过智能技术生成

        关于laravel的登录验证,里面集成了一个auth模块,经过层层逻辑,最终完成了登录验证功能。我一直觉得,它这个操作是不是太麻烦了,登录验证自己写的话,也就几行代码搞定了,何必搞的人晕头转向,欢迎广大网友一起讨论。但人家这么弄了,我们也要研究研究。

      我要达到的目的很简单,就是登录的时候,不光要验证用户名和密码,还要验证我们上篇文章加的一个字段。这种情况在实际开发中,很常见,比如一个人已经被禁止登录了,我们除了判断账户密码,还要验证其有没有被禁止登录。

     登录用的是auth模块。通过facade引入auth:

    protected static function getFacadeAccessor()
    {
        return 'auth';
    }

    其中的auth,实际是Illuminate/Auth/AuthManager.php。绑定地方:

    protected function registerAuthenticator()
    {
        $this->app->singleton('auth', function ($app) {
            // Once the authentication service has actually been requested by the developer
            // we will set a variable in the application indicating such. This helps us
            // know that we

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel提供了多种登录验证方式,包括基于表单认证、基于API认证、基于社交媒体认证等。 以下是基于表单认证的登录验证示例: 1. 创建认证控制器 可以使用Artisan命令快速生成: ``` php artisan make:auth ``` 这将创建一个控制器和视图文件,用于用户登录、注册、密码找回等操作。 2. 配置路由 在routes/web.php文件中添加以下路由: ``` Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); Route::post('login', 'Auth\LoginController@login'); Route::post('logout', 'Auth\LoginController@logout')->name('logout'); ``` 3. 配置认证驱动 在config/auth.php文件中配置认证驱动,例如使用Eloquent模型: ``` 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ], ``` 4. 配置用户模型 在User模型中实现Authenticatable接口,例如: ``` use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { // ... } ``` 5. 配置登录视图 在resources/views/auth/login.blade.php视图文件中添加登录表单,例如: ``` <form method="POST" action="{{ route('login') }}"> @csrf <div class="form-group row"> <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <div class="col-md-6 offset-md-4"> <div class="form-check"> <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}> <label class="form-check-label" for="remember"> {{ __('Remember Me') }} </label> </div> </div> </div> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Login') }} </button> @if (Route::has('password.request')) <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> @endif </div> </div> </form> ``` 6. 实现登录逻辑 在Auth\LoginController控制器中实现登录逻辑,例如: ``` use Illuminate\Http\Request; class LoginController extends Controller { public function login(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended('/'); } return back()->withErrors([ 'email' => 'The provided credentials do not match our records.', ]); } } ``` 在上述代码中,首先使用$request->only()方法获取请求参数,然后使用Auth::attempt()方法进行认证,如果认证成功则调用$request->session()->regenerate()方法重新生成会话ID,最后使用redirect()方法跳转到指定页面(例如/表示首页),如果认证失败则使用back()方法返回上一页,并通过withErrors()方法返回错误信息。 7. 添加中间件 如果需要在用户访问某些页面时进行登录验证,可以添加中间件: ``` Route::get('profile', function () { // 只有已登录用户才能访问该页面 })->middleware('auth'); ``` 在上述代码中,只有已登录用户才能访问/profile页面,否则将被重定向到登录页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值