Laravel 5.5 登录错误未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47104941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:54:52  来源:igfitidea点击:

Laravel 5.5 Login errors not showing up

phplaravellaravel-5blade

提问by funerr

Here is my login.blade.php

这是我的 login.blade.php

@if(Session::get('errors')||count( $errors ) > 0)
   @foreach ($errors->all() as $error)
      <h1>{{ $error }}</h1>
  @endforeach
@endif

Here is my LoginController.php:

这是我的 LoginController.php:

protected function sendFailedLoginResponse(Request $request)
{
    return redirect()->back()
        ->withInput($request->only($this->username(), 'remember'))
        ->withErrors([
            $this->username() => 'ERRORS',
        ]);
}

And here is my web.php (routes)

这是我的 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');
});

When I try to login with a bad user it shows no errorsin the view, what am I doing wrong?

当我尝试使用坏用户登录时,它在视图中没有显示任何错误,我做错了什么?

Update:
I've tried to change the login.blade.php, as @Seva Kalashnikov suggested, with no luck.
I've also tried @Akshay Kulkarni suggestion with no luck.

更新
我尝试更改 login.blade.php,正如@Seva Kalashnikov 所建议的那样,但没有成功。
我也尝试过@Akshay Kulkarni 的建议,但没有成功。

采纳答案by funerr

Ok, after a few hours I finally found it! I created a Laravel project from scratch and made a diff to find the culprit:

好吧,几个小时后我终于找到了!我从头开始创建了一个 Laravel 项目,并做了一个差异来找到罪魁祸首:

In app/Http/Kernel.php, make sure to get rid of the StartSession middleware:

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
];

Explanation: I had it there because I read that I had to put it as a middleware (if I wasn't using the Route::group(['middleware' =>'web']wrapper in my web.php), I think that I forgot it there. I think that putting it there andusing the wrapper in web.php somehow truncate the error session before it gets to the view.

解释:我把它放在那里是因为我读到我必须把它作为中间件(如果我没有Route::group(['middleware' =>'web']在我的 web.php 中使用包装器),我想我忘记了它。我认为把它放在那里在 web.php 中使用包装器以某种方式在它到达视图之前截断错误会话。

回答by Seva Kalashnikov

Try to remove Session::get('errors')from your ifstatement in login.blade.php

尝试Session::get('errors')iflogin.blade.php 中的语句中删除

@if(count( $errors ) > 0)
    @foreach ($errors->all() as $error)
       <h1>{{ $error }}</h1>
    @endforeach
@endif

ShareErrorsFromSessionmiddleware, which is provided by the web middleware group is responsible for $errorview variable so it will always be defined (link here)

ShareErrorsFromSession中间件,由 web 中间件组提供,负责$error视图变量,因此它将始终被定义(链接在这里

[UPDATE]

[更新]

And as @Ohgodwhy pointed, you need to use @if ($errors->any())Example

正如@Ohgodwhy 所指出的,你需要使用@if ($errors->any())Example

So in your case it will be:

所以在你的情况下,它将是:

@if($errors->any())
    @foreach ($errors->all() as $error)
       <h1>{{ $error }}</h1>
    @endforeach
@endif

回答by Akshay Kulkarni

Put,

放,

Auth::routes();

身份验证::路由();

Inside middleware group.

中间件组内部。

Web middleware starts the session. If you are writing any route outside that middleware group then you can not access the session.

Web 中间件启动会话。如果您在该中间件组之外编写任何路由,则无法访问会话。

回答by Melih

If you use Entrust (or maybe some other packages) and add its classes to $routeMiddleware, the problem can be deriving from that your subsequently added custom classes override the default Laravel classes.

如果您使用 Entrust(或者可能是其他一些包)并将其类添加到$routeMiddleware,则问题可能源于您随后添加的自定义类覆盖了默认的 Laravel 类。

The solution is to move your custom classes to the top of $routeMiddlewarearray.

解决方案是将您的自定义类移动到$routeMiddleware数组的顶部。