未定义的变量:Laravel中的错误 [英] Undefined variable: errors in Laravel
问题描述
当我想在laravel项目中注册用户时,页面总是显示
When I want to register a user in my laravel project, the page always says
未定义变量:错误(查看:/var/www/resources/views/auth/register.blade.php)"
Undefined variable: errors (View: /var/www/resources/views/auth/register.blade.php)"
根据Laravel文档,应始终自动设置$errors
:
According to the Laravel documentation, $errors
should always automatically be set:
因此,需要注意的是,在每个请求的所有视图中,始终都会使用$ errors变量,这使您可以方便地假定$ errors变量始终处于定义状态并且可以安全地使用.
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
或其他任何我想使用$errors
变量的方式.
or any other way when I want to use the $errors
variable.
这是为什么?我以前从未遇到过这个问题.
Why is this? I never had this problem before.
有人可以帮我吗?
推荐答案
您应确保在web
的middlewareGroups
属性的app/Http/Kernel.php
中,您拥有:
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
编辑
似乎您需要为要使用的路由添加'middleware' => 'web'
或将\Illuminate\View\Middleware\ShareErrorsFromSession::class,
放入$middleware
属性数组
It seems you need to add 'middleware' => 'web'
for route you are using or put \Illuminate\View\Middleware\ShareErrorsFromSession::class,
into $middleware
property array
在routes.php文件内部,尝试在以下代码段中创建路由
Inside of the routes.php file try to create your routes within the following block
Route::group(['middleware' => ['web']], function () {
//routes here
});
更新版本的laravel应用程序
请注意,如果两次使用web
中间件,也可能会遇到问题. Laravel应用程序 5.2.27 发生了变化(请不要将其与当前使用的Laravel框架相混淆-您可以使用Laravel框架(例如5.2.31),但在5.2版中具有Laravel应用程序. 24),其中web
中间件自动应用于所有路由.因此,如果出现问题,您应该打开app/Providers/RouteServiceProvider.php
文件并验证其内容.
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 :
用于Laravel应用程序5.2.27的RouteServiceProvider
如果您有较新的版本(自动应用web
中间件),则不应再在routes.php
中使用web
中间件,否则应修改RouteServiceProvider
方法以不应用web
组中间件.否则,如果web
中间件组自动应用在此提供程序中,并且在routes.php
中也使用了它,则可能会得到非常意外的结果.
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屋!