这是我的login.blade.php
@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\LoginController@login')->name('login');
});
当我尝试使用不良用户登录时,在视图中显示没有错误,我做错了什么?
更新:
我试图更改login.blade.php,正如@Seva Kalashnikov建议的那样,没有运气。
我也没试过@Akshay Kulkarni的建议。
答案 0 :(得分:2)
尝试从login.blade.php中的Session::get('errors')
语句中删除if
@if(count( $errors ) > 0)
@foreach ($errors->all() as $error)
<h1>{{ $error }}</h1>
@endforeach
@endif
由Web中间件组提供的 ShareErrorsFromSession
中间件负责$error
视图变量,因此始终定义它(link here)
[UPDATE]
正如@Ohgodwhy指出的那样,你需要使用@if ($errors->any())
Example
所以在你的情况下它将是:
@if($errors->any())
@foreach ($errors->all() as $error)
<h1>{{ $error }}</h1>
@endforeach
@endif
答案 1 :(得分:1)
穿戴,
验证::路线();
内部中间件组。
Web中间件启动会话。 如果您在该中间件组之外编写任何路由,则无法访问该会话。
答案 2 :(得分:1)
好的,几个小时后我终于找到了!我从零开始创建了一个Laravel项目,并找到了一个差异来找到罪魁祸首:
在 app / 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中的包装器会以某种方式截断错误会话,然后才能进入视图。
答案 3 :(得分:1)
如果您使用Entrust(或者某些其他软件包)并将其类添加到$routeMiddleware
,则问题可能源于您随后添加的自定义类覆盖默认的Laravel类。
解决方案是将您的自定义类移动到$routeMiddleware
数组的顶部。
答案 4 :(得分:0)
我遇到了同样的问题。经过四处探索,我从'web'的 $ middlewareGroups 中删除了 \ Illuminate \ Session \ Middleware \ StartSession :: clas 在app \ http \ kernel.php中。
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class, // <-- Remove this
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
这是针对@halfer解决方案的。