StartSession中间件清除验证错误

时间:2018-02-09 17:29:42

标签: php laravel session error-handling localization

我正在使用本地化,并且必须将\Illuminate\Session\Middleware\StartSession::class,添加到我的内核中。

我之前关于它的问题here

但现在我的$errors始终为空,并且错误未显示在视图中。

我也尝试在StartSession之后将\Illuminate\View\Middleware\ShareErrorsFromSession::class,添加到我的内核,但错误仍然是空的。

控制器:

public function postRegister(Request $request) {
        $request_data = $request->all();
        $request->validate([
          'first-name'      => 'required',
          'last-name'       => 'required',
        ]);
        //other processing stuff
}

内核

<?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\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\Localization::class,
    ];

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

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

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

我读了这样的错误{{ $errors->first('first-name') }}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

我认为您将StartSessionShareErrorsFromSession添加到错误的变量中,它们不仅仅是HTTP个中间件,而是某些中间件组的功能,例如。

Web是一个中间件组,其中包含各种较小的中间件,这些中间件丰富了Request左右的其他可用功能,如使用CookieSessionCsrfToken等。< / p>

尝试从StartSession变量中删除ShareErrorsFromSession$middleware

编辑:同时向我们展示您的Localization.php代码,谢谢

答案 1 :(得分:1)

我遇到了同样的问题,我在StartSession Localization数组中添加了$middlewareGroups类然后web类,而不是$middleware 因为如果你把它放在$middleware(执行每个HTTP请求)中,旧会话将被删除,然后创建一个新会话。