php - Laravel 中间件取消请求并保持页面处于相同状态
问题描述
这是我当前的问题。
目前,我有一个页面,其中包含可以通过 AJAX 添加和附加的元素。这些元素包含表单、图片上传等。
我的整个应用程序上有一个中间件,可以检查在任何给定时间上传的任何图像的大小并确保其小于 5MB(应用程序上每个图像上传的图像验证不是一个选项,它必须是 1 个控制器来维护所有图片上传验证)。
如果请求检测到超过 5MB 的图像,它将运行此代码
return redirect()->back()->withInput($request->all())->withErrors(array('Image' => 'Sorry, ' . $file->getClientOriginalName() . ' is too large, maximum file size is 5MB. Please reduce the size of your image!'));
这段代码非常喜怒无常,这就是为什么。
当它返回时,我需要页面处于与我离开它的状态完全相同的状态。这意味着所有 AJAX 加载的元素、所有图像、所有内容都需要处于相同状态,因此redirect()->back()->withInput($request->all())
不起作用,因为它仍会刷新页面并删除在该实例中添加和添加的所有加载内容。
如果请求失败,我需要能够取消请求。
简单来说,当这个中间件运行时,检测所有图像。如果有超过 5MB 的图像,请不要刷新页面或任何内容。只是错误
我知道这看起来很愚蠢,因为请求无法在不刷新的情况下将某些内容传回,但我认为我会询问/接受建议。
这是我的中间件
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
class ImageInterceptor
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
foreach (array_filter(array_flatten($request->files->all())) as $file) {
//Check if the file being uploaded is not a csv
if($file->getClientOriginalExtension() != 'csv'){
$filename = $file->getClientOriginalName();
$size = $file->getClientSize(); // size in bytes!
$onemb = pow(1024, 2);
if ($size > $onemb * 5) {
//Return back, image is too big!
return redirect()->back()->withInput($request->all())->withErrors(array('Image' => 'Sorry, ' . $file->getClientOriginalName() . ' is too large, maximum file size is 5MB. Please reduce the size of your image!'));
}
}
}
return $next($request);
}
}
解决方案
如果您计划让页面处于相同状态,那么您不能告诉它在出错时向后重定向,您将不得不返回一个数组、字符串或任何您需要的东西。通过说向后重定向,它告诉浏览器导航到哪里。
关于维护输入,您可以尝试以下几行:
<input type="text" name="firstname" id="firstname" class="form-control" value="{{ $user->firstname or old('firstname') }}">
为什么不创建表单请求?我真的怀疑你需要对你需要的每个页面进行验证。在我看来,中间件应该处理身份验证和授权。
表单请求将类似于:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class Example extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'photo' => 'required|mimes:jpeg,bmp,png|size:5000'
];
}
}
在您的控制器上,您只需在函数上放置一个参数(而不是 Request $request,而是放置 Example $request)。这样,您可以访问 Illuminate 拥有的每个请求信息以及您自己的验证。
推荐阅读
- flask - 我如何在不同的端口上运行 Swagger UI,然后在 Flask 中的主 App 端口上运行?就像 Swagger 在 8080 上运行,主应用在 8081 上运行
- excel - 从引用中删除前导零
- python - 如何将曲线拟合到包含 NaN 值的 Excel 文件的数据
- javascript - 如何在带有scrollspy的导航系统中保持css中的元素处于活动状态?
- lisp - Common Lisp 排序函数从原始列表中删除一个元素
- python - 使用 sklearn 预测离散值
- javascript - 在反应挂钩中同步调用函数后数据未加载?
- python - 我将如何计算这个 python 程序的运行时间、内存和 CPU 使用率?
- debugging - 模拟器中的 Apple Id 登录授权
- c - linux中的参数到argv