Laravel 5.2.45 - 在视图

时间:2016-11-07 21:34:20

标签: laravel

问题:

视图中的$ errors变量为空。有人说过这个问题已在5.2中得到修复,所以希望这个问题在我的最后。

环境:

  • Mac OS X
  • Laravel 5.2.45

The Codez:

routes.php文件

Route::get('/', 'AlleleController@index');

Route::get('/register', function () {
    return view('auth.register');
});

Route::auth();

Route::get('/home', 'HomeController@index');

Route::get('/alleles', 'AlleleController@index');
Route::post('/allele', 'AlleleController@store');
Route::delete('/allele/{allele}', 'AlleleController@destroy');

AlleleController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Allele;

class AlleleController extends Controller {

    /**
    * Create a new controller instance.
    *
    * @return void
    */
    public function __construct() {
        // All methods require authentication except index.
        $this->middleware('auth', ['except' => ['index']]);
    }

    /**
    * Root page.
    *
    * @return Response
    */
    public function index() {
        return view('welcome');
    }

    /**
    * Create a new allele.
    *
    * @param  Request  $request
    * @return Response
    */
    public function store(Request $request) {
        $allele = new Allele();
        // Get all input as an array.
        $input = $request->all();
        // Validate input.
        if ($allele->validate($input)) {
            // Valid input. Write to database.
            // The inserted model instance is returned.
            $result = $allele::create($input);
            if ($result) {
                // Insert successful.
                $message = array('message' => 'Data added!');
                return view('home', $message);
            } else {
                // Insert failed. Send errors to view.
                $errors = array('errors' => 'Error saving data.');
                return view('home', $errors);
            }
        } else {
            // Invalid input. Get errors.
            $errors = $allele->errors();
            // Send errors to view.
            return view('home', $errors);
        }
    }
}
?>

HomeController.php:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;

class HomeController extends Controller {
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct() {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        return view('home');
    }
}

型号:Allele.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Allele extends Validation {

    /**
    * The attributes that are mass assignable.
    */
    protected $fillable = ['allele'];

    /**
    * Validation rules.
    */
    protected $rules = array (
        'allele' => 'required|max:20',
    );
}

型号:Validation.php

<?php

namespace App;

use Validator;
use Illuminate\Database\Eloquent\Model;

class Validation extends Model {

    protected $rules = array();

    protected $errors;

    public function validate($input) {
        // Make a new validator object.
        $v = Validator::make($input, $this->rules);

        // Check for failure.
        if ($v->fails()) {
            // Set errors and return false.
            $this->errors = $v->errors();
            return false;
        }

        // Validation passed.
        return true;
    }

    // Retrieves the errors object.
    public function errors() {
        return $this->errors;
    }
}

查看:views / common / errors.blade.php

@if (count($errors) > 0)
    <!-- Form Error List -->
    <div class="alert alert-danger">
        <strong>Whoops! Something went wrong!</strong>
        <br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

查看:views / home.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    You are logged in!
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Create New Allele -->
<div class="panel-body">
    <!-- Display Validation Errors -->
    @include('common.errors')

    <!-- New Allele Form -->
    <form action="{{ url('allele') }}" method="POST" class="form-horizontal">
        {{ csrf_field() }}

        <!-- Allele Name -->
        <div class="form-group">
            <label for="allele-name" class="col-sm-3 control-label">Allele</label>
            <div class="col-sm-6">
                <input type="text" name="allele" id="allele-name" class="form-control">
            </div>
        </div>

        <!-- Add Allele Button -->
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-6">
                <button type="submit" class="btn btn-default">
                    <i class="fa fa-plus"></i> Add Allele
                </button>
            </div>
        </div>
    </form>
</div>
@endsection

1 个答案:

答案 0 :(得分:0)

所有验证方法都应放在web middleware内。我没有看到任何其他错误。像这样替换你的route.php

Route::group(['middleware' => ['web']], function ()
{
Route::get('/', 'AlleleController@index');

Route::get('/register', function () {
    return view('auth.register');
});

Route::auth();

Route::get('/home', 'HomeController@index');

Route::get('/alleles', 'AlleleController@index');
Route::post('/allele', 'AlleleController@store');
Route::delete('/allele/{allele}', 'AlleleController@destroy');
});