我使用验证请求的验证方法,但视图中的var错误为空:/。
在控制器中,我有:
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' ]);
});
});
对于控制器,我有:
<?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);
}
}
对于视图,我有:
@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
如果删除路由的中间件,则var错误可以正常工作。
我怎么能解决这个问题?
感谢。
答案 0 :(得分:0)
如果您使用的是最新的Laravel 5.2版本,则应从routes
中删除double
中间件。现在它会自动应用于所有路线,如果您尝试手动添加,则会导致不同的错误。