Laravel 5.5 未显示登录错误
Laravel 5.5 Login errors not showing up
这是我的 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');
});
当我尝试使用错误用户登录时,在视图中没有显示任何错误,我做错了什么?
更新:
我尝试按照@Seva Kalashnikov 的建议更改 login.blade.php,但没有成功。
我也尝试过@Akshay Kulkarni 的建议,但没有成功。
尝试从 login.blade.php
中的 if
语句中删除 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
放,
Auth::routes();
内部中间件组。
Web 中间件启动会话。
如果您在该中间件组之外编写任何路由,那么您将无法访问该会话。
好的,几个小时后我终于找到了!我从头开始创建了一个 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 中的包装器以某种方式在它到达视图之前截断错误会话。
如果您使用 Entrust(或其他一些软件包)并将其 类 添加到 $routeMiddleware
,问题可能源于您随后添加的自定义 类 覆盖了默认值Laravel 类.
解决方案是将您的自定义 类 移动到 $routeMiddleware
数组的顶部。
我遇到了同样的问题。在这里和那里进行了大量挖掘之后,我通过从 $middlewareGroups 中删除 \Illuminate\Session\Middleware\StartSession::class 解决了 [=15] =]'web' 在 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 的解决方案。
这是我的 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');
});
当我尝试使用错误用户登录时,在视图中没有显示任何错误,我做错了什么?
更新:
我尝试按照@Seva Kalashnikov 的建议更改 login.blade.php,但没有成功。
我也尝试过@Akshay Kulkarni 的建议,但没有成功。
尝试从 login.blade.php
中的if
语句中删除 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
放,
Auth::routes();
内部中间件组。
Web 中间件启动会话。 如果您在该中间件组之外编写任何路由,那么您将无法访问该会话。
好的,几个小时后我终于找到了!我从头开始创建了一个 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 中的包装器以某种方式在它到达视图之前截断错误会话。
如果您使用 Entrust(或其他一些软件包)并将其 类 添加到 $routeMiddleware
,问题可能源于您随后添加的自定义 类 覆盖了默认值Laravel 类.
解决方案是将您的自定义 类 移动到 $routeMiddleware
数组的顶部。
我遇到了同样的问题。在这里和那里进行了大量挖掘之后,我通过从 $middlewareGroups 中删除 \Illuminate\Session\Middleware\StartSession::class 解决了 [=15] =]'web' 在 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 的解决方案。