laravel 5使用请求的验证不起作用-未定义的变量错误


laravel 5 validation using request not working - Undefined variable error

我使用的是Laravel 5.2,我有表单,希望插入带有验证的字段。我在请求文件夹下创建了QuranRequest,并试图回显验证错误,但我得到了以下错误

a19890dff92858726bf2b1048815af329d53d3b6.php第6行出现错误异常:未定义的变量:错误(视图:/Applications/MAMP/htdocs/quran/resources/views/pages/quranFormblade.php)(查看:/Applications/MAMP/htdocs/quran/resources/views/pages/quranFormblade.php)

我的quranForm.blade.php代码,我试图在其中吐出错误

<div class="form-group">
  {!!Form::label('title','Surah Title:')!!}    
  {!!Form::text('title',null,['class' => 'form-control'])!!} 
  <span class="help-block">{{$errors->first('title') }}</span>
 </div>
 <div class="form-group">
   {!!Form::label('lyrics','Surah Lyrics:')!!}       
   {!!Form::textarea('lyrics',null,['class' => 'form-control'])!!}  
 </div>
 <div class="form-group">
   {!!Form::submit('Add Surah',['class' => 'btn btn-primary'])!!}
 </div>

我的createQuranRequest文件

<?php
namespace App'Http'Requests;
use App'Http'Requests'Request;
class createQuranRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required',
            'lyrics' => 'required'
        ];
    }
}

我的控制器文件

public function store(createQuranRequest $request, quran $quran){ 
  $quran->create($request->all());
  return redirect('quran');
}

我试过下面的代码

@if (Session::get('errors'))
  <ul>
    @foreach ($errors->all() as $error)
      <li>{ { $error } }</li>
    @endforeach
  </ul>
@endif   

它删除了异常错误,但不显示错误。

laravel文档说$errors变量通过Illuminate''view''Middleware''ShareErrorsFromSession中间件绑定到视图,该中间件由web中间件组提供。

所以你必须更新你的路线文件位于应用程序/http/routes.php

Route::group(['middleware' => ['web']], function () 
{
    //this route will use the middleware of the 'web' group, so session and auth will work here         
    Route::post('/your-endpoint','MyController@store');       
});