Laravel 5.2 错误消息未显示

Posted

技术标签:

【中文标题】Laravel 5.2 错误消息未显示【英文标题】:Laravel 5.2 error message not shown 【发布时间】:2016-09-29 04:04:05 【问题描述】:

我已经找到了一些答案,但我的问题仍然没有解决。

我刚做了一个新的应用程序,发现注册页面上的错误消息根本没有显示。 我按照我在这个网站上找到的一些答案如下:

    只需删除 , 'middleware' => 'web' 从 Route::group(array('prefix' => 'user', 'middleware' => 'web'), function() 在 routes.php 页面

    移动

\Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,

从受保护的 $middlewareGroups 到 karnel.php 页面中受保护的 $middleware

(感谢 Atikur)

单击注册按钮时弹出致命错误:致命错误:在字符串上调用成员函数 first()(查看:C:\xampp\htdocs\laravel\resources\views\auth\register.blade.php )

这是我的一些代码供参考。请帮忙。

routes.php

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

Route::group(['prefix' => 'user'], function () 

    Route::get('/', 'UserController@index');

    Route::post('/', 'UserController@update');

    Route::post('/edit', 'UserController@edit');
);


Route::auth();

Route::get('/home', 'HomeController@index');

Route::get('/test', 'TestController@index');

kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel

    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];
/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller

    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    
        return Validator::make($data, [
            'name' => 'required|unique:users|min:3|max:20|alpha_dash',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|confirmed',
        ]);
    

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    

registered.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Register</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action=" url('/register') ">
                        !! csrf_field() !!

                        <div class="form-group $errors->has('name') ? ' has-error' : '' ">
                            <label class="col-md-4 control-label">Name</label>

                            <div class="col-md-6">
                                <input type="text" class="form-control" name="name" value=" old('name') ">

                                @if (count($errors) > 0)
                                    <div class="alert alert-danger">
                                        <ul>
                                            @foreach ($errors->all() as $error)
                                                <li> $error->first('name') </li>
                                            @endforeach
                                        </ul>
                                    </div>
                                @endif
                            </div>
                        </div>

                        <div class="form-group $errors->has('email') ? ' has-error' : '' ">
                            <label class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value=" old('email') ">

                                @if (count($errors) > 0)
                                    <div class="alert alert-danger">
                                        <ul>
                                            @foreach ($errors->all() as $error)
                                                <li> $error->first('email') </li>
                                            @endforeach
                                        </ul>
                                    </div>
                                @endif
                            </div>
                        </div>

                        <div class="form-group $errors->has('password') ? ' has-error' : '' ">
                            <label class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">

                                @if (count($errors) > 0)
                                    <div class="alert alert-danger">
                                        <ul>
                                            @foreach ($errors->all() as $error)
                                                <li> $error->first('password') </li>
                                            @endforeach
                                        </ul>
                                    </div>
                                @endif
                            </div>
                        </div>

                        <div class="form-group $errors->has('password_confirmation') ? ' has-error' : '' ">
                            <label class="col-md-4 control-label">Confirm Password</label>

                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password_confirmation">

                                @if (count($errors) > 0)
                                    <div class="alert alert-danger">
                                        <ul>
                                            @foreach ($errors->all() as $error)
                                                <li> $errors->first('password_confirmation') </li>
                                            @endforeach
                                        </ul>
                                    </div>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-user"></i>Register
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

php工匠-v

Laravel Framework version 5.2.35

【问题讨论】:

可以显示register.blade.php的内容吗? 您使用的究竟是什么版本? php artisan -v 在您更好地理解您的应用程序之前,不要到处移动这些中间件类。在整个应用程序中随机弹出TokenValidationExceptions 可能会导致一些非常严重的错误。花了 2 个月的时间确定我们的一个软件堆栈中存在这个问题。 @Jonathon 也更新我的 register.blade.php @AlexeyMezenin 它是 5.2.35,也在帖子中更新 【参考方案1】:

在 Blade 中试试这个

 <div class=" form-group has-feedback $errors->has('name') ? ' has-error' : '' ">
            <input type="text" class="form-control" name="name" value=" old('name') "
                   placeholder="Full Name">

            @if ($errors->has('name'))
                <span class="help-block">
                    <strong> $errors->first('name') </strong>
                </span>
            @endif
        </div>`<div class="form-group has-feedback $errors->has('email') ? ' has-error' : '' ">
            <input type="email" class="form-control" name="email" value=" old('email') "
                   placeholder="Email">

            @if ($errors->has('email'))
                <span class="help-block">
                    <strong> $errors->first('email') </strong>
                </span>
            @endif
        </div>`<div class="col-md-6">
                <div class="form-group has-feedback $errors->has('password') ? ' has-error' : '' ">
                    <input type="password" class="form-control" name="password" placeholder="Password">

                    @if ($errors->has('password'))
                        <span class="help-block">
                    <strong> $errors->first('password') </strong>
                </span>
                    @endif
                </div>
            </div>

【讨论】:

感谢您的意见。我在我的帖子中更新了我的 register.blade,我认为代码与你的代码类似?但它仍然没有显示。请输入更多谢谢。【参考方案2】:

将此代码插入register.blade.php

@if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li> $error </li>
        @endforeach
    </ul>
</div>
@endif

【讨论】:

也感谢您的意见。我在帖子中更新了我的 register.blade 代码,请检查。我认为我的工作方式与您的类似,但仍然没有显示错误消息。请帮忙!

以上是关于Laravel 5.2 错误消息未显示的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.2 未找到基表或视图错误

Laravel 5.2 & AJAX - 重定向后显示成功消息

Laravel 5.2 中的“PhpDebugBar 未定义”错误

未找到 Laravel 视图 5.2

Laravel 5.2:在用户登录/注销和注册后显示会话闪烁消息

Laravel 5.2 单元测试错误:BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::make()