未定义变量:Laravel 中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34454081/
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
Undefined variable: errors in Laravel
提问by Anhinga
When I want to register a user in my laravel project, the page always says
当我想在我的 Laravel 项目中注册用户时,页面总是说
Undefined variable: errors (View: /var/www/resources/views/auth/register.blade.php)"
未定义变量:错误(查看:/var/www/resources/views/auth/register.blade.php)”
According to the Laravel documentation, $errors
should always automatically be set:
根据 Laravel 文档,$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.
因此,重要的是要注意 $errors 变量在每个请求的所有视图中始终可用,允许您方便地假设 $errors 变量始终已定义并且可以安全使用。
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.
或者当我想使用$errors
变量时的任何其他方式。
Why is this? I never had this problem before.
为什么是这样?我以前从未遇到过这个问题。
Can someone help me please?
有人能帮助我吗?
回答by Marcin Nabia?ek
You should make sure that in app/Http/Kernel.php
in middlewareGroups
property for web
you have:
你应该确保在app/Http/Kernel.php
在middlewareGroups
属性web
您有:
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
in this array. Compare this with https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php
在这个数组中。将此与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
看来你需要添加'middleware' => 'web'
你所使用的路线或将\Illuminate\View\Middleware\ShareErrorsFromSession::class,
进入$middleware
属性数组
or
或者
Inside of the routes.php file try to create your routes within the following block
在 routes.php 文件中,尝试在以下块中创建您的路由
Route::group(['middleware' => ['web']], function () {
//routes here
});
UPDATE FOR NEWER VERSIONS OF LARAVEL APPLICATION
更新 Laravel 应用程序的新版本
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.
请注意,如果您web
两次使用中间件,您也可能会遇到问题。Laravel 应用程序5.2.27(不要将它与您目前使用的 Laravel 框架混淆 - 您可能使用 Laravel 框架,例如 5.2.31,但在版本 5.2.24 中使用 Laravel 应用程序),其中web
应用了中间件自动为所有路线。因此,如果出现问题,您应该打开app/Providers/RouteServiceProvider.php
文件并验证其内容。
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.
如果你有更新的版本(web
自动应用中间件),你不应该再使用web
中间件,routes.php
或者你应该修改你的RouteServiceProvider
方法来不应用web
组中间件。否则,如果web
在此提供程序中自动应用中间件组并且您也在其中使用它,routes.php
您可能会得到非常意外的结果。
回答by Felipe Pe?a
I had this very same issue with Laravel 5.2.x.
我在 Laravel 5.2.x 上遇到了同样的问题。
Inside of the routes.php
file try yo create your routes within the
在routes.php
文件内部尝试创建您的路线
Route::group(['middleware' => ['web']], function () {
//routes here
}
statement.
陈述。
回答by Krzysztof Boduch
Also to be aware of: If you write tests and your view has$errors variable make sure you don't use WithoutMiddlewaretrait.
还要注意:如果您编写测试并且您的视图具有$errors 变量,请确保您不使用withoutMiddleware特性。
回答by Tom11
I had similar problem and solved this one by adding routes into middleware property array as well,
我遇到了类似的问题,并通过将路由添加到中间件属性数组中来解决这个问题,
BUT
但
it worked only after calling php artisan route:cache
(clearing route cache) subsequently.
它仅在php artisan route:cache
随后调用(清除路由缓存)后才起作用。
I hope some of you would find this useful.
我希望你们中的一些人会发现这很有用。
回答by tisuchi
Go to App\Http\Kernel.phpfile. Move all the things of $middlewareGroups
properties to $middleware
.
转到App\Http\Kernel.php文件。将所有$middlewareGroups
属性的东西移到$middleware
.
Check for more details- http://www.tisuchi.com/laravel-5-2-undefined-variable-error-validation/
检查更多细节 - http://www.tisuchi.com/laravel-5-2-undefined-variable-error-validation/
回答by Nikush
I was seeing this error too and later realised I had used the WithoutMiddleware
trait as a means to bypass authentication for this particular test, but it ended up removing the validation error binding too. So I had to stop using the trait to keep the views working.
我也看到了这个错误,后来意识到我已经使用这个WithoutMiddleware
特征来绕过这个特定测试的身份验证,但它最终也删除了验证错误绑定。所以我不得不停止使用这个特性来保持视图的工作。
回答by Vojko
count is not really realiable since it assumes the variable already exists. change the condition check to: @if($errors->has())
or just @if($errors)
count 并不是真的可行,因为它假设变量已经存在。将条件检查更改为:@if($errors->has())
或只是@if($errors)
Also if you are redirecting make sure to use this in your controller
此外,如果您要重定向,请确保在您的控制器中使用它
return redirect()->back()->with('errors', $validator->messages());
EDIT: seen now that you are using L5.2 This may answer your question - you need to put your Routes in Route group.
编辑:现在看到您正在使用 L5.2 这可能会回答您的问题 - 您需要将您的路线放在路线组中。
回答by Amit Rai
protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Social\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Social\Http\Middleware\VerifyCsrfToken::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
],
'api' => [
'throttle:60,1',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
make your kernel look like this
让你的内核看起来像这样
回答by Asadullah Al Galib
Your problem will be fixed by using this method.
使用此方法将解决您的问题。
Route::group(['middleware' => ['web']], function () {
//routes should go here
});
If this doesn't help you, just run the following artisan command in addition to the above code:
如果这对您没有帮助,除了上面的代码外,只需运行以下 artisan 命令:
php artisan key:generate
I solved in this way while using 5.2.*
我在使用 5.2.* 时以这种方式解决了
回答by Brijesh Hadiyal
It was not actually error, it was warning you can remove using php error_reporting()
function
这实际上不是错误,而是警告您可以使用 phperror_reporting()
函数删除
I removed warning in my Laravel 5.6 by using
我通过使用删除了 Laravel 5.6 中的警告
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL ^ E_NOTICE);
More Detail : http://php.net/manual/en/function.error-reporting.php
更多细节:http: //php.net/manual/en/function.error-reporting.php
Hope it helps.
希望能帮助到你。