Laravel 5.5登录错误没有显示出来
这里是我的login.blade.phpLaravel 5.5登录错误没有显示出来
@if(Session::get('errors')||count($errors) > 0)
@foreach ($errors->all() as $error)
<h1>{{ $error }}</h1>
@endforeach
@endif
这里是我的LoginController.php:
protected function sendFailedLoginResponse(Request $request)
{
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors([
$this->username() => 'ERRORS',
]);
}
这里是我的web.php(路线)
// I am customizing the login to do extra checks,
// but I still need the basic auth scaffolding.
Auth::routes();
...
Route::group(['middleware' => 'web'], function() {
Route::view('/login', 'auth.login');
Route::post('/login', 'Auth\[email protected]')->name('login');
});
当我试图用错误的用户它显示视图中没有错误登录,我算什么我做错了?
更新:
我试图改变login.blade.php,作为@Seva卡拉什尼科夫认为,没有运气。
我也试过@Akshay Kulkarni建议没有运气。
好吧,几个小时后我终于找到了!我创建了一个从无到有的Laravel项目,并提出了差异找到罪魁祸首:
在应用程序/ HTTP/Kernel.php,一定要摆脱StartSession中间件:
protected $middleware = [
\Illuminate\Foundation\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,
\Illuminate\Session\Middleware\StartSession::class, // <-- Remove this
];
说明:我在那里,因为我读过,我不得不把它作为中间件(如果我没有在我的web.php中使用Route::group(['middleware' =>'web']
包装),我认为我忘了它。我认为把它放在那里和使用web.php中的包装以某种方式截断错误会话,然后它进入视图。
尝试从if
声明login.blade.php删除Session::get('errors')
@if(count($errors) > 0)
@foreach ($errors->all() as $error)
<h1>{{ $error }}</h1>
@endforeach
@endif
ShareErrorsFromSession
中间件,它是由Web中间件组提供负责$error
视图变量,因此它总是被定义(link here )
[更新]
而作为@Ohgodwhy指出,你需要使用@if ($errors->any())
Example
所以你的情况将是:
@if($errors->any())
@foreach ($errors->all() as $error)
<h1>{{ $error }}</h1>
@endforeach
@endif
如果您使用Entrust(或者其他一些软件包)并将它们的类添加到$routeMiddleware
,则问题可能源于随后添加的自定义类覆盖默认的Laravel类。
解决方案是将您的自定义类移动到$routeMiddleware
数组的顶部。
它应该是'$ errors-> any()' – Ohgodwhy
下面是来自Laravel Basic Auth Views的示例:https://github.com/drbyte/laravel-basic-auth-views/blob/master/resources/views /auth/login.blade.php –
和[这里是来自documentaion的示例](https://laravel.com/docs/5.5/validation#quick-displaying-the-validation-errors)。它应该是'$ errors-> any()'。查看留言包始终可用。 – Ohgodwhy