我正在尝试处理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
是空的。
我的重定向有问题吗?
谢谢各位帮忙!
The ViewErrorBag
为空,因为会话未在Handler
。解决方案之前描述过@Talinon at Laracast
使会话可用Handler
上课,我搬家了\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class
from $middleware
to $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,
],
...
];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)