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

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

问题描述

我是Laravel的新手,并且使用laravel 5.2版.

I am new in Laravel and using laravel version 5.2.

我创建了一个控制器,并将请求分别命名为 ArticlesController CreateArticleRequest ,并定义了一些验证规则.

I created a controller and request named as ArticlesController and CreateArticleRequest respectively and i defined some validation rules.

CreateArticleRequest

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class CreateArticleRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|min:3',
            'body' => 'required|max:400',
            'published_at' => 'required|date',
        ];
    }
}

ArticlesController

<?php

namespace App\Http\Controllers;

use App\Article;
//use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\Http\Requests\CreateArticleRequest;

class ArticlesController extends Controller
{

    public function add(){
        return view('articles.add');
    }

    public function create_article_row(CreateArticleRequest $request){
        Article::create($request->all());
        return redirect('articles/');
    }
}

当我在名为 add.blade.php 的模板中使用$ errors变量时,它显示错误未定义变量:$ errors 我试图解决问题,但没有解决.请告诉我哪里错了. add.blad.php

When i use $errors variable in my template named as add.blade.php it show error undefined variable: $errors I tried to solve the problem but i did't .Please tell me where i am wrong . add.blad.php

{{ var_dump($errors) }}

推荐答案

这是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文件(app/Http/Kernel.php)中,可以将middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class移回protected $middleware属性.

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

用路由组包装所有web路由,然后将Web中间件应用于它们:

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...(Cut all `Route` which are define in `Route file`, paste here) 
});

从此帖子中复制 Laravel 5.2 $ Blade中未出现错误

这篇关于未定义的变量:错误-Laravel 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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