我正在使用本地化,并且必须将\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') }}
我做错了什么?
答案 0 :(得分:2)
我认为您将StartSession
和ShareErrorsFromSession
添加到错误的变量中,它们不仅仅是HTTP
个中间件,而是某些中间件组的功能,例如。
Web是一个中间件组,其中包含各种较小的中间件,这些中间件丰富了Request
左右的其他可用功能,如使用Cookie
,Session
,CsrfToken
等。< / p>
尝试从StartSession
变量中删除ShareErrorsFromSession
和$middleware
。
编辑:同时向我们展示您的Localization.php
代码,谢谢
答案 1 :(得分:1)
我遇到了同样的问题,我在StartSession
Localization
数组中添加了$middlewareGroups
类然后web
类,而不是$middleware
因为如果你把它放在$middleware
(执行每个HTTP请求)中,旧会话将被删除,然后创建一个新会话。