dsfdsf8888 2016-04-03 23:03
浏览 330
已采纳

使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀