【问题标题】:Laravel middleware cancel request and keep page in same stateLaravel 中间件取消请求并保持页面处于相同状态
【发布时间】:2018-10-27 13:39:39
【问题描述】:

这是我当前的问题。

目前,我有一个页面,其中包含可以通过 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 的图像,请不要刷新页面或任何内容。只是错误

我知道这看起来很愚蠢,因为请求无法在不刷新的情况下将某些内容传回,但我认为 id 询问/接受建议。

这是我的中间件

<?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);
    }
}

【问题讨论】:

  • 进行 ajax 调用以验证所选图像,如果通过则提交表单。(这是一个猜测,不知道该怎么做!)

标签: php laravel laravel-5 laravel-validation laravel-request


【解决方案1】:

如果您计划让页面处于相同状态,那么您不能告诉它在出现错误时向后重定向,您必须返回一个数组、字符串或任何您需要的东西。通过说向后重定向,它告诉浏览器导航到哪里。

关于维护输入,您可以尝试以下几行:

<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

【讨论】:

  • 感谢您的建议,但我不得不通过中间件进行操作的原因是我继承了项目,并且使用图像上传的控制器太多了。我不认为我可以通过所有这些,并将它们全部发送到请求中,但我不能冒险错过一个。对我来说,全球中间件似乎是最好的选择
  • 我已经编辑了帖子,尝试将 old('value') 添加到输入中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2016-03-23
  • 2020-03-20
  • 2020-09-16
相关资源
最近更新 更多