刀片中无法访问请求错误(Laravel 5.2) [英] Request Errors not accessible in blade (Laravel 5.2)

查看:64
本文介绍了刀片中无法访问请求错误(Laravel 5.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我使用laravel已经好几个月了,但是从未遇到过这样的问题.

It had been many months since I'm using laravel but never faced such problem.

我制作了一个简单的Request类来验证更新用户请求,如果遵循验证规则,该类就可以正常工作.如果验证规则失败,我们应该返回上一页并以html显示所有错误.

I have made a simple Request class to validate the the update user request which works fine if validation rules are followed. If validation rule fails we should come back to the previous page and display all errors in html.

根据我的说法,我已经像以前在其他应用程序中一样正确地编写了所有内容,但是 $errors在刀片中似乎无法访问

According to me I have written everything correctly as I used to write in other applications but the $errors seems to be inaccessible in blade

以下是我需要调试的代码段:

Following are my required code snippets to debug:

routes.php

Route::group(['middleware' => ['web']], function () {
    Route::get('/users/{id}/edit', 'UserController@edit');
    Route::post('/users/{id}/edit', 'UserController@update');
});

UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\UserUpdateRequest;
use App\Models\User;
use App\Models\Role;
use App\Models\Post;

class UserController extends Controller
{    
    public function edit($id)
    {
        try {
            $user = User::find($id);
            $roles = Role::all();
            return view('users.edit', compact(['user', 'roles']));
        }catch(Exception $e) {
            return view('errors.500', compact(['e']));
        }
    }

    public function update($id, UserUpdateRequest $request)
    {
        dd($request);
    }
}

UserUpdateRequest.php

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UserUpdateRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name'      =>  'required|string|min:4',
            'email'     =>  'required|email',
            'role'      =>  'required|numeric',
            'password'  =>  'required',
        ];
    }
}

edit.blade.php

@extends('master')

@section('title') Edit Users @stop

@section('content')
<div class="row">
    <div class="col-sm-12">
        <h2>Edit User</h2>
    </div>
</div>
<div class="alert alert-warning alert-dismissible" role="alert">
  @foreach($errors->all() as $error)
  {{ $error }}
  @endforeach
</div>
<form action="{{ url('/users/'.$user->id.'/edit') }}" method="post">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="col-sm-6">
        <div class="form-group">
            <label>Name</label>
            <input type="text" name="name" value="{{ $user->name }}" class="form-control" placeholder="Name">
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label>Email Address</label>
            <input type="text" name="email" value="{{ $user->email }}" class="form-control" placeholder="Email Address">
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label>Role</label>
            <select name="role" class="form-control">
            @foreach($roles as $role)
            @if($role->id == $user->role)
            <option value="{{ $role->id }}" selected>{{ $role->name }}</option>
            @else
            <option value="{{ $role->id }}">{{ $role->name }}</option>
            @endif
            @endforeach
            </select>
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label>Password</label>
            <input type="password" name="password" class="form-control" placeholder="New Password">
        </div>
    </div>
    <div class="col-sm-12">
        <div class="form-group">
            <input type="submit" class="btn btn-info btn-block" value="Update">
        </div>
    </div>
</form>
@stop

浏览器上的HTML响应为空白. 我还尝试了<?php dd($errors); ?>显示以下内容

The HTML response on browser is blank. I also tried <?php dd($errors); ?> which displayed the following

Edit User

ViewErrorBag {#168 ▼
  #bags: []
}

更多信息此处

推荐答案

@VipindasKS是正确的.从Laravel版本5.2.28开始,Web中间件通过RouteServiceProviders的方法包含在所有路由中:

@VipindasKS is right with his assumption. Since Laravel Version 5.2.28 the web middleware is included in all routes via the RouteServiceProviders's method:

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web',
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}

由于该版本Laravel的默认route.php文件仅包含:

Since that version Laravel's default routes.php file only contains:

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

因此,如果您从以前的版本升级,则该文件具有一个如下所示的route.php文件:

So if you upgrade from a previous version, that has a routes.php file like this:

Route::group(['middleware' => ['web']], function () {
   // web routes
});

您的应用程序将正常运行,因为使用作曲家更新时,您不会触摸RouteServiceProvider(它不会添加mapWebRoutes()方法).因此,网络"中间件仅添加到网络"组中的路由中.

Your application will just work fine, because with an composer update you won't touch your RouteServiceProvider (It does not add the mapWebRoutes() method). So the 'web' middleware is only added to the routes within the 'web' group'.

但是,如果您要重新安装Laravel的最新安装(当前为5.2.29),并使用

However if you are pulling a fresh installation of Laravel ( currently 5.2.29 ) and have a routes.php with

Route::group(['middleware' => ['web']], function () {
   // web routes
});

Web中间件堆栈将被添加到路由两次.您可以通过以下方式进行检查:

The web middleware stack will be added twice to the routes. You can check this via:

php artisan route:list

这将显示两次'web'中间件被添加:

Which will show that the 'web' middleware is added twice:

| POST      | users/{id}/edit          |                  | App\Http\Controllers\UserController@update      | web,web    |

这会中断会话的flash变量,因为它们通常仅打算在一个会话生命周期内持续使用.

This breaks the Session's flash variables as they are normally only intended to last only during one session lifecycle.

因此解决方案是:

如果您使用的是"routes.php"文件中的网络"中间件组,请不要使用 拉了一个新的laravel实例.

Don't use the 'web' middleware group in the routes.php file if you pulled a fresh instance of laravel.

这篇关于刀片中无法访问请求错误(Laravel 5.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆