未定义变量:错误 -- Laravel 5.2
Undefined variable: errors -- Laravel 5.2
我是 Laravel 的新手,正在使用 laravel 5.2 版。
我分别创建了一个名为 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',
];
}
}
文章控制器
<?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 变量时,它显示错误 undefined variable: $errors
我试图解决问题,但我没有。请告诉我哪里错了。
add.blad.php
{{ var_dump($errors) }}
使用此代码,您可以捕获错误并显示它们:
@if ($errors->any())
<div class='alert alert-danger'>
@foreach ( $errors->all() as $error )
<p>{{ $error }}</p>
@endforeach
</div>
@endif
这是 5.2 升级的重大问题。发生的事情是负责使 errors
变量可用于所有视图的中间件未被使用,因为它已从全局中间件移至 web
中间件组。
有两种方法可以解决这个问题:
在您的 kernel.php
文件 (app/Http/Kernel.php) 中,您可以将 middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
移回 protected $middleware
属性.
用路由组包装所有 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
只需从 routes.php 文件中剪切所有路由并将其粘贴到中间件组 'web' 之间,就像这样:
张贴此内容可能对其他人有用,
正如 Praveen 在第一个解决方案中提到的那样,在您的 Kernel.php
文件 (app/Http/Kernel.php
) 中移动 \Illuminate\View\Middleware\ShareErrorsFromSession::class
从 $middlewareGroups
到 protected $middleware
属性,但同样会开始抛出错误 "Session store not set on request"、
解决这个举动
\Illuminate\Session\Middleware\StartSession::class,
到 $middleware property
。
对于 5.2,只需将具有错误变量的路由移动到中间件组
发生这种情况是因为下面的文件没有在 composer 更新过程中更新,所以没有实现 mapWebRoutes
方法。
app/Providers/RouteServiceProvider.php
从全新安装中复制此文件即可。更好的是,按照文档上的升级路径进行操作。
我是 Laravel 的新手,正在使用 laravel 5.2 版。
我分别创建了一个名为 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',
];
}
}
文章控制器
<?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 变量时,它显示错误 undefined variable: $errors 我试图解决问题,但我没有。请告诉我哪里错了。 add.blad.php
{{ var_dump($errors) }}
使用此代码,您可以捕获错误并显示它们:
@if ($errors->any())
<div class='alert alert-danger'>
@foreach ( $errors->all() as $error )
<p>{{ $error }}</p>
@endforeach
</div>
@endif
这是 5.2 升级的重大问题。发生的事情是负责使 errors
变量可用于所有视图的中间件未被使用,因为它已从全局中间件移至 web
中间件组。
有两种方法可以解决这个问题:
在您的
kernel.php
文件 (app/Http/Kernel.php) 中,您可以将middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
移回protected $middleware
属性.用路由组包装所有
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
只需从 routes.php 文件中剪切所有路由并将其粘贴到中间件组 'web' 之间,就像这样:
张贴此内容可能对其他人有用,
正如 Praveen 在第一个解决方案中提到的那样,在您的 Kernel.php
文件 (app/Http/Kernel.php
) 中移动 \Illuminate\View\Middleware\ShareErrorsFromSession::class
从 $middlewareGroups
到 protected $middleware
属性,但同样会开始抛出错误 "Session store not set on request"、
解决这个举动
\Illuminate\Session\Middleware\StartSession::class,
到 $middleware property
。
对于 5.2,只需将具有错误变量的路由移动到中间件组
发生这种情况是因为下面的文件没有在 composer 更新过程中更新,所以没有实现 mapWebRoutes
方法。
app/Providers/RouteServiceProvider.php
从全新安装中复制此文件即可。更好的是,按照文档上的升级路径进行操作。