首页 > 解决方案 > 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);
    }
}

标签: phplaravellaravel-5laravel-validationlaravel-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 拥有的每个请求信息以及您自己的验证。

https://laravel.com/docs/5.2/validation#rule-mimetypes


推荐阅读