php 未定义变量:错误——Laravel 5.2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34474224/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:00:49  来源:igfitidea点击:

Undefined variable: errors -- Laravel 5.2

phplaravellaravel-5.2

提问by Muhammad Asif Saleem

I am new in Laravel and using laravel version 5.2.

我是 Laravel 的新手,使用的是 Laravel 5.2 版。

I created a controller and request named as ArticlesControllerand CreateArticleRequestrespectively and i defined some validation rules.

我分别创建了一个名为ArticlesControllerCreateArticleRequest的控制器和请求,并定义了一些验证规则。

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/');
    }
}

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

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

{{ var_dump($errors) }}

Click here to see Error Image

单击此处查看错误图像

回答by smartrahat

This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errorsvariable available to all your views is not being utilized because it was moved from the global middleware to the webmiddleware group.

这是 5.2 升级的一个重大问题。发生的事情是负责使该errors变量可用于所有视图的web中间件未被使用,因为它已从全局中间件移至中间件组。

There are two ways to fix this:

有两种方法可以解决这个问题:

  1. In your kernel.phpfile(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::classback to the protected $middlewareproperty.

  2. Wrap all your webroutes 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) 
    });
    
  1. 在您的kernel.php文件(app/Http/Kernel.php)中,您可以将其middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class移回protected $middleware属性。

  2. web用一个路由组包裹你的所有路由,并将 web 中间件应用到它们:

    Route::group(['middleware' => 'web'], function() {
        // Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here) 
    });
    

Copied from this post Laravel 5.2 $errors not appearing in Blade

复制自这篇文章Laravel 5.2 $errors 未出现在 Blade 中

回答by Sambhav Pandey

Posting this as it might be useful for others,

发布此信息,因为它可能对其他人有用,

As Praveen mentioned in 1st solution, in your Kernel.phpfile(app/Http/Kernel.php) move \Illuminate\View\Middleware\ShareErrorsFromSession::classfrom $middlewareGroupsto protected $middlewareproperty, but the same will start throwing the error "Session store not set on request",

正如 Praveen 在第一个解决方案中提到的,在您的Kernel.phpfile( app/Http/Kernel.php) 中\Illuminate\View\Middleware\ShareErrorsFromSession::class$middlewareGroupstoprotected $middleware属性移动,但同样会开始抛出错误“会话存储未按请求设置”,

to resolve this move \Illuminate\Session\Middleware\StartSession::class,to $middleware propertyas well.

要解决这一举动 \Illuminate\Session\Middleware\StartSession::class,$middleware property为好。

回答by SuperDuck

This happens because the file below is not updated in the composer update process, so doesn't have the mapWebRoutesmethod implemented.

发生这种情况是因为下面的文件没有在 composer 更新过程中更新,所以没有mapWebRoutes实现该方法。

app/Providers/RouteServiceProvider.php

Copy this file over from a fresh install and it will work. Better, follow the upgrade path on the docs.

从全新安装复制此文件,它将起作用。更好的是,按照文档上的升级路径进行操作。

回答by RobertP

Just cut all your routes from routes.php file and paste it between the middleware group 'web', just like this:

只需从 routes.php 文件中剪切所有路由并将其粘贴到中间件组“web”之间,就像这样:

Routes file

路由文件

回答by Koko

For 5.2, simply move the the routes that have the errors variable to middleware group

对于 5.2,只需将具有错误变量的路由移动到中间件组

回答by Kokno

With this code, you can catch errors and display them :

使用此代码,您可以捕获错误并显示它们:

@if ($errors->any())
 <div class='alert alert-danger'>
  @foreach ( $errors->all() as $error )
   <p>{{ $error }}</p>
  @endforeach
 </div>
@endif