未定义变量:Laravel 中的错误 [英] Undefined variable: errors in Laravel

查看:25
本文介绍了未定义变量:Laravel 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

未定义变量:错误(查看:/var/www/resources/views/auth/register.blade.php)"

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

<块引用>

因此,重要的是要注意 $errors 变量将始终在您对每个请求的所有视图中可用,让您可以方便地假设 $errors 变量始终已定义并且可以安全使用.

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

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

@万一

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

这是为什么?我以前从未遇到过这个问题.

有人可以帮我吗?

解决方案

你应该确保在 app/Http/Kernel.php 中的 middlewareGroups 属性为 web 你有:

IlluminateViewMiddlewareShareErrorsFromSession::class,

在这个数组中.将此与 https://github.com/laravel/laravel/进行比较blob/master/app/Http/Kernel.php

编辑

看来您需要添加 'middleware' =>'web' 用于您正在使用的路由或将 IlluminateViewMiddlewareShareErrorsFromSession::class, 放入 $middleware 属性数组

在 routes.php 文件中尝试在以下块中创建路由

Route::group(['middleware' => ['web']], function () {//这里的路由});

更新 Laravel 应用程序的新版本

请注意,如果您两次使用 web 中间件,也可能会遇到问题.Laravel 应用程序 5.2.27 发生了变化(不要将它与您目前使用的 Laravel 框架混淆 - 您可能使用 Laravel 框架,例如 5.2.31,但在 5.2 版中有 Laravel 应用程序.24) 其中 web 中间件自动应用于所有路由.因此,如果出现问题,您应该打开您的 app/Providers/RouteServiceProvider.php 文件并验证其内容.

你也可以在这里比较:

如果你有更新的版本(自动应用 web 中间件),你不应该再在 routes.php 中使用 web 中间件或者您应该修改您的 RouteServiceProvider 方法以不应用 web 组中间件.否则,如果 web 中间件组自动应用在此提供程序中并且您也在 routes.php 中使用它,您可能会得到非常意外的结果.

When I want to register a user in my laravel project, the page always says

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

According to the Laravel documentation, $errors should always automatically be set:

So, it is important to note that an $errors variable will always be available in all of your views on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used.

I have this on on every view when I use:

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

or any other way when I want to use the $errors variable.

Why is this? I never had this problem before.

Can someone help me please?

解决方案

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

IlluminateViewMiddlewareShareErrorsFromSession::class,

in this array. Compare this with 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 IlluminateViewMiddlewareShareErrorsFromSession::class, into $middleware property array

or

Inside of the routes.php file try to create your routes within the following block

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.

You can compare it also here :

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 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆