我刀片中的登录表单可以验证但不会显示错误,如果失败,它将返回空白条目的登录页面,我该如何处理?
最初,我的路由是在api.php中定义的,并且遇到了相同的问题,因此我将其更改为web.php,并且仍然相同。同样value =“ {{old('name')}}”“也不起作用。
这是我在刀片中写的表格
<h5>Login Your Account</h5>
<!-- FORM -->
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="/login" >
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" placeholder="" value="{{old('name')}}">
@if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> @endif
</div>
<div class="form-group ">
<label for="password">Password</label>
<input type="password" id="password" class="form-control" name="password" value="{{old('password')}}">
@if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> @endif
</div>
<li class="col-sm-12 text-left">
<button type="submit" class="btn-round">Login</button>
</li>
</form>
这是我的控制器登录功能
public function login(Request $request){
$validator= Validator::make($request->all(),
['name'=> "bail|required|max:50|exists:users",
'password' => 'required|min:6|exists:users,password'
]
);
if ($validator->fails()){
return redirect()->back()->withInput()->withError($validator);
}
else{
if (Auth::attempt(['name'=> $request->name, 'password'=>$request->password])){
return redirect('/home');
}
}
这是我的web.php路由
Route::get('/', function () {
return view('home');
});
Route::post('/login','API\userController@login');
还有我的kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
/**
* The priority-sorted list of middleware.
*
* This forces non-global middleware to always be in the given order.
*
* @var array
*/
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\Authenticate::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
}
如果验证成功,它将重定向到我指定的路由,但是一旦失败,它将直接返回登录页面,并且不会显示错误
答案 0 :(得分:2)
Laravel的RedirectResponse类具有方法withErrors()
,用于在您调用withError()
的地方发送验证程序消息包。
请参见Laravel的RedirectResponse.php
/**
* Flash a container of errors to the session.
*
* @param \Illuminate\Contracts\Support\MessageProvider|array|string $provider
* @param string $key
* @return $this
*/
public function withErrors($provider, $key = 'default')
{
$value = $this->parseErrors($provider);
$this->session->flash(
'errors', $this->session->get('errors', new ViewErrorBag)->put($key, $value)
);
return $this;
}