Laravel登录和使用错误代码注册 [英] Laravel Login & Registration with Error Code

查看:68
本文介绍了Laravel登录和使用错误代码注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel登录&使用错误代码注册

Laravel Login & Registration with Error Code

当检测到无效的登录电子邮件或密码时,希望返回错误消息.

Would like to have an error message returned when an invalid login email or password is detected..

我想到的是要有一个控制器来触发错误消息显示在login.php上.推荐吗?

What was on my mind was to have a controller to trigger the error message to appear on the login.php. is it recommended?

中间件/身份验证

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate
{
/**
 * The Guard implementation.
 *
 * @var Guard
 */
protected $auth;

/**
 * Create a new filter instance.
 *
 * @param  Guard  $auth
 * @return void
 */
public function __construct(Guard $auth)
{
    $this->auth = $auth;
}

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    if ($this->auth->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login');
        }
    }




    return $next($request);
}
}

Login.php

Login.php

<!-- resources/views/auth/login.blade.php -->

</style>
<form method="POST" action="login">
{!! csrf_field() !!}

<h1>iMakan | Login</h1>
<div>
    Email
    <input class="form-control" type="email" name="email" value="{{ old('email') }}">
</div>

<div>
    Password
    <input class="form-control" type="password" name="password" id="password" >
</div>

<div>
    <input type="checkbox" name="remember"> Remember Me
</div>

<div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
</div>
<br>
<div>
    <p><a href="register">Sign Up</a></p>
</div>
<div>
    <p><a href="register">Forget Password</a></p>    
</div>
</form>

@stop

推荐答案

您可以使用它来循环错误

You can use this to loop errors

@if(count($errors) > 0)
         <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
         </div>
    @endif

这篇关于Laravel登录和使用错误代码注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆