Laravel 5.2 $errors 没有出现在 Blade 中 [英] Laravel 5.2 $errors not appearing in Blade
问题描述
所以我一直在学习 Laravel 5 基础教程,但我被困在表单验证上.我完全按照教程进行操作,但我收到了一个未定义的变量:在我的创建文章视图中出现错误.
So I'm following along with the Laravel 5 fundamentals tutorial and I am stuck on the form validation. I have followed along exactly with the tutorial but I am getting a Undefined variable: errors in my create articles view.
在我正在关注的教程以及我在网上找到的内容中,他们说刀片文件中始终存在错误变量供您使用,所以我不知道我做错了什么?
In the tutorial I am following and what I have found online they say the errors variable is always there in the blade file for you to use so I don't know what i am doing wrong?
任何帮助将不胜感激!喜欢 Laravel 除了这个错误!
Any help would be appreciated! loving Laravel except for this error!
View
@if($errors->any())
<ul class="alert alert-danger">
@foreach($errors->any() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
控制器
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppUser;
use AppHttpRequests;
use AppHttpRequestsUserRequest as UserRequest;
// use AppHttpRequestsCreateArticleRequest as CreateArticleRequest;
use AppHttpControllersController;
use IlluminateViewMiddlewareErrorBinder;
class UserController extends Controller
{
public function create(){
return view('pages.signUp');
}
public function store(UserRequest $request){
User::create($request->all());
return 'the user has been registered!';
return view('user.profile');
}
}
请求验证
<?php
namespace AppHttpRequests;
use AppHttpRequestsRequest;
class UserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'country' => 'required',
'password' => 'required|min:6',
'confirm_password' => 'required|same:password',
'height' => 'required',
'weight' => 'required',
];
}
}
推荐答案
这是 5.2 升级的一个严重问题.发生的事情是负责使 errors
变量可用于所有视图的中间件没有被使用,因为它从全局中间件移到了 web
中间件组.
This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors
variable available to all your views is not being utilized because it was moved from the global middleware to the web
middleware group.
有两种方法可以解决这个问题:
There are two ways to fix this:
在您的
kernel.php
文件中,您可以将中间件IlluminateViewMiddlewareShareErrorsFromSession::class
移回protected$middleware
属性.
In your
kernel.php
file, you can move the middlewareIlluminateViewMiddlewareShareErrorsFromSession::class
back to theprotected $middleware
property.
您可以使用路由组包装所有网络路由,并将 web
中间件应用于它们.
You can wrap all your web routes with a route group and apply the web
middleware to them.
Route::group(['middleware' => 'web'], function() {
// Place all your web routes here...
});
这篇关于Laravel 5.2 $errors 没有出现在 Blade 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!