dsfdsf8888
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'); 
})->name('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:

@extends('layouts.master')

@section('content')
    <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">
        <ul>
            @foreach ($errors->all() as $error)
                {!! $errors->first() !!}
            @endforeach
        </ul>
    </div>
        @endif      
        <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>
            </select>
            <input type="text" name="name"> 
            <button type="submit">action</button>
            <input type="hidden" name="_token" value="{{Session::token()}}"> 
        </form>
    </div>
   @endsection

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

how i can fix this?

thanks.

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

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.

    点赞 评论 复制链接分享

相关推荐