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
Undefined variable: errors -- Laravel 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.
我分别创建了一个名为ArticlesController和CreateArticleRequest的控制器和请求,并定义了一些验证规则。
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) }}
回答by smartrahat
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.
这是 5.2 升级的一个重大问题。发生的事情是负责使该errors
变量可用于所有视图的web
中间件未被使用,因为它已从全局中间件移至中间件组。
There are two ways to fix this:
有两种方法可以解决这个问题:
In your
kernel.php
file(app/Http/Kernel.php), you can move themiddleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
back to theprotected $middleware
property.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) });
在您的
kernel.php
文件(app/Http/Kernel.php)中,您可以将其middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
移回protected $middleware
属性。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
回答by Sambhav Pandey
Posting this as it might be useful for others,
发布此信息,因为它可能对其他人有用,
As Praveen mentioned in 1st solution, in your Kernel.php
file(app/Http/Kernel.php
) move \Illuminate\View\Middleware\ShareErrorsFromSession::class
from $middlewareGroups
to protected $middleware
property, but the same will start throwing the error "Session store not set on request",
正如 Praveen 在第一个解决方案中提到的,在您的Kernel.php
file( app/Http/Kernel.php
) 中\Illuminate\View\Middleware\ShareErrorsFromSession::class
从$middlewareGroups
toprotected $middleware
属性移动,但同样会开始抛出错误“会话存储未按请求设置”,
to resolve this move
\Illuminate\Session\Middleware\StartSession::class,
to $middleware property
as 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 mapWebRoutes
method 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”之间,就像这样:
回答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