Laravel 5.2 $错误未出现在Blade中 [英] Laravel 5.2 $errors not appearing in Blade

查看:57
本文介绍了Laravel 5.2 $错误未出现在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.

在我正在跟踪的教程中,以及我在网上找到的内容之后,他们说刀片文件中始终存在errors变量供您使用,所以我不知道我在做什么错了?

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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Http\Requests;
use App\Http\Requests\UserRequest as UserRequest;
// use App\Http\Requests\CreateArticleRequest as CreateArticleRequest;
use App\Http\Controllers\Controller;
use Illuminate\View\Middleware\ErrorBinder;

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 App\Http\Requests;

use App\Http\Requests\Request;

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:

  1. 在您的kernel.php文件中,您可以将中间件\Illuminate\View\Middleware\ShareErrorsFromSession::class移回protected $middleware属性.

  1. In your kernel.php file, you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.

您可以使用路由组包装所有Web路由,然后将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 $错误未出现在Blade中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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