当我想在我的laravel项目中注册用户时,页面总是显示

Undefined variable: errors (View: /var/www/resources/views/auth/register.blade.php)"

根据Laravel文档,应始终自动设置$errors:

因此,需要注意的是,对于每个请求,$ERROR变量在您的所有视图中始终可用,从而使您可以方便地假设$ERROR变量总是被定义并且可以安全地使用.

当我使用时,每个视图上都有这个:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

或者当我想使用$errors变量时的任何其他方式.

Why is this? I never had this problem before.

谁能帮帮我吗?

推荐答案

You should make sure that in app/Http/Kernel.php in middlewareGroups property for web you have:

\Illuminate\View\Middleware\ShareErrorsFromSession::class,

在这个数组中.把这个和https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php相比

EDIT

It seems you need to add 'middleware' => 'web' for route you are using or put \Illuminate\View\Middleware\ShareErrorsFromSession::class, into $middleware property array

or

在routes.php文件内部,try 在以下挡路内创建路径

Route::group(['middleware' => ['web']], function () {
    //routes here
});

UPDATE FOR NEWER VERSIONS OF LARAVEL APPLICATION

Be aware that you might run into problems also in case you use web middleware twice. There was a change in Laravel application 5.2.27 (don't confuse it with Laravel framework you use at the moment - you might use Laravel framework for example 5.2.31 but have Laravel application in version 5.2.24) in which web middleware is applied automatically for all routes. So in case of problems, you should open your app/Providers/RouteServiceProvider.php file and verify its content.

您还可以在这里进行比较:

In case you have newer version (that applies web middleware automatically), you shouldn't use web middleware in routes.php anymore or you should modify your RouteServiceProvider method to not apply web group middleware. Otherwise if web middleware group is automatically applied in this provider and you use it also in routes.php you might get very unexpected results.

Laravel相关问答推荐

为什么删除查询执行 Laravel

我的共享主机上的每个帖子请求都出现 503 服务不可用

在 Laravel 中清除 Routes&config:cache 后未定义的常量

Laravel 9:AWS S3 检索要流式传输的视频

如何在 laravel livewire 中按 bool 值排序数组

自定义 Laravel 关系?

刚安装 Lumen 并得到 NotFoundHttpException

file_put_content ...无法打开流:Laravel 5 中的权限被拒绝

Laravel 使用 Storage::put 生成唯一 ID

使用 ModelNotFoundException

何时在 Laravel 中使用 Repository vs Service vs Trait?

Laravel:自定义或扩展通知 - 数据库模型

Laravel 验证 pdf mime

如何在 Laravel 4 中组织不同版本的 REST API 控制器?

在 Laravel artisan 命令中使用详细

Laravel 用户能力

无法在控制器的构造函数上调用 Auth::user()

复合唯一密钥验证 - laravel

使用 Laravel Mix 合并多个文件

Laravel 4:分析器在哪里?