Php 在Laravel 5.5中处理PostTooLargeException

Php 在Laravel 5.5中处理PostTooLargeException,php,redirect,handler,laravel-5.5,Php,Redirect,Handler,Laravel 5.5,我正在尝试在我的Laravel 5.5应用程序中处理PostTooLargeException 当我试图通过我的表单上传过大的文件时,我收到了PostTooLargeException,我在app\Exceptions\Handler.php中成功捕获了它,但在这一步,我想将用户重定向回表单页面并显示错误消息 我编写了以下代码: class Handler extends ExceptionHandler { ... public function render($request, Ex

我正在尝试在我的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');
            }
    ...
    }
}
结果,我被重定向到正确的页面,但没有任何消息,
viewerrbag
为空。 我的重定向有什么问题吗


谢谢你的帮助

ViewErrorBag为空,因为会话未在
处理程序中启动。解决方案之前由at描述

为了使会话在
处理程序
类中可用,我将
\illumb\Foundation\Http\Middleware\ValidatePostSize::class
$Middleware
移动到
App/Http/Kernel.php的
$middlewareGroups
数组中

我更新的
$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,

        ],
        ...
];
受保护的$middlewareGroups=[
“网络”=>[
\App\Http\Middleware\EncryptCookies::class,
\Illumb\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illumb\Session\Middleware\StartSession::class,

\Illumb\Foundation\Http\Middleware\ValidatePostSize::class,//您是否检查
刀片
中是否有一些
会话
消息?例如
@if(会话::has('foo'){{$foo}@endif
@Tarasovych是的,我有,我没有任何会话消息。现在我看到,当出现
PostTooLargeException
时,会话还没有启动,所以我需要以某种方式启动会话…
\session::start()
session\u start()
对我没有帮助,但我还没有用这种方式挖掘太多内容…嗯,非常奇怪,
会话尚未启动…它似乎起作用了,但抛出了一个错误
PHP警告:第0行的帖子内容长度8978294字节超过了未知的8388608字节限制
。根据您的回答,我添加了
\lightlight\Session\Middleware\StartSession::将
类转换为
$middleware
,它成功了。谢谢