未定义的变量:错误-Laravel 5.2 [英] Undefined variable: errors -- 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:
-
在您的
kernel.php
文件(app/Http/Kernel.php)中,可以将middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
移回protected $middleware
属性.
In your
kernel.php
file(app/Http/Kernel.php), you can move themiddleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
back to theprotected $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屋!