Laravel登录&注册错误代码

时间:2015-11-16 15:15:30

标签: php laravel authentication login

Laravel登录&注册错误代码

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

我的想法是让控制器触发出现在login.php上的错误消息。是推荐吗?

中间件/验证

<?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

<!-- 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

1 个答案:

答案 0 :(得分:1)

您可以使用它来循环错误

@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
相关问题