Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

php - Handling PostTooLargeException in Laravel 5.5

I am trying to handle PostTooLargeException in my Laravel 5.5 application.

When I trying to upload too big file through my form I receive PostTooLargeException which I successfully catch in appExceptionsHandler.php, but on that step I would like to redirect user back to the page with form and show an error message.

I wrote following code:

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

As a result I was redirected to the proper page but without any message and ViewErrorBag was empty. Did I something wrong with that redirection?

Thank you a help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The ViewErrorBag is empty because session is not start in the Handler. Solution was previously described by @Talinon at Laracast

To make session available in the Handler class, I moved IlluminateFoundationHttpMiddlewareValidatePostSize::class from $middleware to $middlewareGroups array at the App/Http/Kernel.php

My updated $middlewareGroups array looks like:

protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            IlluminateFoundationHttpMiddlewareValidatePostSize::class, // <<< this line was added
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,

        ],
        ...
];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...