我正在尝试处理 PostTooLargeException
在我的 Laravel 5.5 应用程序中。
当我尝试通过表单上传太大的文件时,我收到 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
是空的。我的重定向有问题吗?
谢谢你的帮助!
最佳答案
ViewErrorBag
为空,因为 session 未在 Handler
中启动.解决方案之前由 @Talinon 描述过在 Laracast
使 session 在 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,
],
...
];
关于php - 在 Laravel 5.5 中处理 PostTooLargeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49650234/