2016-04-03 23:03 阅读 299

使用laravel框架的视图中的var $ errors

i use the validated method for validate request, but the var errors is empty in the view :/.

in the controller, i have:

Route::group(['middleware' => ['web']], function () {

Route::get('/', function(){
    return view('home'); 

Route::group(['prefix' => 'do'], function($name = null){

Route::get('/{action}/{name?}', ['uses' => 'controllers@get', 'as' =>                                                                  'get']); 

    Route::post('/', ['uses' => 'controllers@post', 'as' => 'post' ]);


for the controller, i have:

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class controllers extends Controller

  public function get($action, $name=null)
   return view('actions.' . $action, ['name' => $name]);


  public function post(Request $request)

 $this->validate($request, [
     'action' => 'required',
     'name' => 'alpha|required'

 return view('actions.'.$request['action'] , ['action' =>        $request['action'], 'name'=>$this->transformName($request['name'])]);  

private function transformName($name)
  $add = "king ";
   return $add.strtoupper($name);

and for the view, i have:


    <div class="centered">
        <a href="{{route('get', ['action' => 'greet'])}}">greet</a> 
        <a href="{{route('get', ['action' => 'hug'])}}">hug</a>
        <a href="{{route('get', ['action' => 'kiss'])}}">kiss</a>
        <br >
        <br >
         @if (isset($errors) && count($errors) > 0)
       <div class="alert alert-danger">
            @foreach ($errors->all() as $error)
                {!! $errors->first() !!}
        <form action="{{ route('post')}}" method="post">
            <label for="select-action">I want to...</label>
            <select id="select-action" name="action">  
                <option value="greet">greet</option>
                <option value="hug">hug</option>
                <option value="kiss">kiss</option>
            <input type="text" name="name"> 
            <button type="submit">action</button>
            <input type="hidden" name="_token" value="{{Session::token()}}"> 

if i delete the middleware of the route, the var errors work correctly.

how i can fix this?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doujianwei8217 doujianwei8217 2016-04-04 03:44

    If you're using latest Laravel 5.2 build, you should remove web middleware from routes. Now it applies automatically to all routes and if you're trying to add it manually, it causes different errors.

    点赞 评论 复制链接分享