如何在 Laravel 中捕获 PostTooLargeException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42883154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch PostTooLargeException in Laravel?
提问by doncadavona
To reproduce the error, simply upload a file(s) to any POST routes in Laravel that exceeds the post_max_size
in your php.ini
configuration.
要重现错误,只需上传一个文件(S)在Laravel任何邮路超出post_max_size
你的php.ini
配置。
My goal is to simply catch the error so I can inform the user that the file(s) he uploaded is too large. Say:
我的目标是简单地捕获错误,以便通知用户他上传的文件太大。说:
public function postUploadAvatar(Request $request)
try {
// Do something with $request->get('avatar')
// Maybe validate file, store, whatever.
} catch (PostTooLargeException $e) {
return 'File too large!';
}
}
The above code is in standard Laravel 5 (PSR-7). The problem with it is that the function can't execute once an error occurs on the injected request. Thereby can't catch it inside the function. So how to catch it then?
上面的代码在标准 Laravel 5 (PSR-7) 中。它的问题在于一旦注入的请求发生错误,该函数就无法执行。从而无法在函数内部捕获它。那么如何捕捉呢?
回答by Rwd
Laravel uses its ValidatePostSize
middleware to check the post_max_size
of the request and then throws the PostTooLargeException
if the CONTENT_LENGTH
of the request is too big. This means that the exception if thrown way before it even gets to your controller.
Laravel 使用它的ValidatePostSize
中间件来检查post_max_size
请求的 ,然后PostTooLargeException
如果CONTENT_LENGTH
请求过大则抛出。这意味着如果在它到达您的控制器之前抛出异常。
What you can do is use the render()
method in your App\Exceptions\Handler
e.g.
你可以做的是render()
在你的App\Exceptions\Handler
eg中使用该方法
public function render($request, Exception $exception)
{
if ($exception instanceof PostTooLargeException) {
return response('File too large!', 422);
}
return parent::render($request, $exception);
}
Please note that you have to return a response from this method, you can't just return a string like you can from a controller method.
请注意,您必须从此方法返回响应,不能像从控制器方法中那样只返回字符串。
The above response is to replicate the return 'File too large!';
you have in the example in your question, you can obviously change this to be something else.
上面的回答是复制return 'File too large!';
您在问题示例中的内容,您显然可以将其更改为其他内容。
Hope this helps!
希望这可以帮助!
回答by bishop
You can also redirect to a Laravel view of your choice if you wish to add a more content richer response to the user.
如果您希望向用户添加更多内容、更丰富的响应,您还可以重定向到您选择的 Laravel 视图。
public function render($request, Exception $exception)
{
if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException)
return response()->view('errors.post-too-large');
return parent::render($request, $exception);
}
ALSO NOTE:For the exception to be caught you should make sure you provide the proper path to the PostTooLargeException
by either importing the class using the use Illuminate\Http\Exceptions\PostTooLargeException;
import statement or just writing the full path like in my example.
另请注意:对于要捕获的异常,您应该确保PostTooLargeException
通过使用use Illuminate\Http\Exceptions\PostTooLargeException;
import 语句导入类或仅像我的示例中那样编写完整路径来提供正确的路径 。