StartSession中间件清除验证错误

StartSession中间件清除验证错误

本文介绍了StartSession中间件清除验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was working with localization and had to add \Illuminate\Session\Middleware\StartSession::class, to my kernel.

我以前关于它的问题此处.

但是现在我的$errors始终为空,并且错误不会显示在视图中.

But now my $errors is always empty and the errors don't show up in view.

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

I also tried adding \Illuminate\View\Middleware\ShareErrorsFromSession::class, after StartSession to my kernel but the errors are still empty.

控制器:

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') }}

我在做什么错了?

推荐答案

我认为您正在将StartSessionShareErrorsFromSession添加到错误的变量中,它们并不是单独的HTTP中间件,而是特定组的功能例如中间件.

I think you are adding StartSession and ShareErrorsFromSession into a wrong variable, they are not alone HTTP middlewares, but rather features of a certain group of middleware, for example.

Web是一个中间件组,其中包括各种较小的中间件,这些中间件丰富了围绕Request演变的更多可用功能,例如使用CookieSessionCsrfToken等.

Web is a middleware group which includes various smaller middleware's which enrich further usable features which evolve around Request, like using Cookie, Session, CsrfToken, etc.

尝试从$middleware变量中删除StartSessionShareErrorsFromSession.

编辑:也请向我们显示您的Localization.php代码,谢谢

Also show us your Localization.php code, thanks

这篇关于StartSession中间件清除验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:00