我正在尝试在Laravel 5.5应用程序中处理PostTooLargeException
。
当我尝试通过我的表单上传过大的文件时,我收到PostTooLargeException
,我成功收到了app\Exceptions\Handler.php
,但在此步骤中,我想将用户重定向回带有表单并显示的页面错误信息。
我写了以下代码:
class Handler extends ExceptionHandler
{
...
public function render($request, Exception $exception)
{
...
if($exception instanceof PostTooLargeException){
return redirect()->back()->withErrors("Size of attached file should be less ".ini_get("upload_max_filesize")."B", 'addNote');
}
...
}
}
因此,我被重定向到正确的页面,但没有任何消息,ViewErrorBag
为空。
我的重定向有问题吗?
谢谢你的帮助!
答案 0 :(得分:0)
ViewErrorBag
为空,因为会话未在Handler
开始。解决方案之前由@Talinon Laracast
要在Handler
课程中提供会话,我在\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class
$middleware
从$middlewareGroups
移至App/Http/Kernel.php
数组
我更新的$middlewareGroups
数组如下:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, // <<< this line was added
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
...
];