Php 我使用的是Laravel 5';我不清楚如何实现验证器类

Php 我使用的是Laravel 5';我不清楚如何实现验证器类,php,validation,laravel,laravel-5,laravel-validation,Php,Validation,Laravel,Laravel 5,Laravel Validation,我正在使用,不清楚如何实现验证器类 我想创建一个ResizeImageCommandValidator类,在尝试调整图像大小之前检查图像是否确实是图像 我想从ResizeImageCommandHandler resize方法中提取代码 if (!($image instanceof Image)) { throw new ImageInvalidException('ResizeImageCommandHandler'); } 这个想法来自Laracasts,但Jeffrey没有使用

我正在使用,不清楚如何实现验证器类

我想创建一个ResizeImageCommandValidator类,在尝试调整图像大小之前检查图像是否确实是图像

我想从ResizeImageCommandHandler resize方法中提取代码

if (!($image instanceof Image))
{
    throw new ImageInvalidException('ResizeImageCommandHandler');
}
这个想法来自Laracasts,但Jeffrey没有使用Laravel5架构

这是密码

ResizeImageCommandHandler.php

<?php namespace App\Handlers\Commands;

use App\Commands\ResizeImageCommand;

use App\Exceptions\ImageInvalidException;
use Illuminate\Queue\InteractsWithQueue;
use Intervention\Image\Image;

class ResizeImageCommandHandler {

    /**
     * Create the command handler.
     */
    public function __construct()
    {
    }
    /**
     * Handle the command.
     *
     * @param  ResizeImageCommand  $command
     * @return void
     */
    public function handle($command)
    {
        $this->resizeImage($command->image, $command->dimension);
    }
    /**
     * Resize the image by width, designed for square image only
     * @param Image $image Image to resize
     * @param $dimension
     * @throws ImageInvalidException
     */
    private function resizeImage(&$image, $dimension)
    {
        if (!($image instanceof Image))
        {
            throw new ImageInvalidException('ResizeImageCommandHandler');
        }
        $image->resize($dimension, null, $this->constrainAspectRatio());
    }
    /**
     * @return callable
     */
    private function constrainAspectRatio()
    {
        return function ($constraint) {
            $constraint->aspectRatio();
        };
    }


}      
<?php namespace App\Commands;

use App\Commands\Command;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use Image;

class ResizeImageCommand extends Command {
    use InteractsWithQueue, SerializesModels;

    public $image;
    public $savePath;
    public $dimension;

    /**
     * Create a new command instance.
     * @param Image $image
     * @param string $savePath
     * @param int $dimension
     * @param int $pose_id
     * @param int $state_id
     */
    public function __construct(&$image, $savePath, $dimension)
    {
        $this->image = $image;
        $this->savePath = $savePath;
        $this->dimension = $dimension;
    }

}

您可以使用Laravel 5的
FormRequest
类在请求发送到命令总线之前捕获请求:

public function postResizeImage(ResizeImageRequest $request) {
    // Send request to the Command Bus
}
然后,在您的
ResizeImageRequest
类中,将规则放入其中,您可能需要自定义验证以验证上载的文件是否实际上是图像

您可以使用来处理图像文件。或者你可以用这个


如果您需要进一步的帮助,请询问我。为了回答您的问题,我建议不要在命令部分挂断电话。在Laravel 5.1中,该文件夹被重命名为“作业”-参考

这正是因为泰勒觉得人们太拘泥于“命令”这个词了

另见和

Illumb包中的验证器类非常好,-我想我不确定这有什么问题

我要说的是,除非您有充分的理由为此使用命令类,否则不要这样做。另见:

我谦虚地建议你可能问错了问题,也许你不需要使用命令来处理这个问题

这可能就是你想要的答案:

未解决时使用illumb\Contracts\Validation\validates

如果这不起作用,那就报名参加拉腊喀特——这是一个为这类事情提供的松弛渠道。有史以来最适合拉威尔帮忙的地方。(当然,堆栈溢出除外)

这里有一个用于检查图像格式的小类,我想您可能会发现它很有用

<?php
Class FileUploadFormat
{
public function is_image($image_path)
  {
      if (!$f = fopen($image_path, 'rb'))
      {
          return false;
      }

      $data = fread($f, 8);
      fclose($f);

      // signature checking
      $unpacked = unpack("H12", $data);
      if (array_pop($unpacked) == '474946383961' || array_pop($unpacked) == '474946383761') return "gif";
      $unpacked = unpack("H4", $data);
      if (array_pop($unpacked) == 'ffd8') return "jpg";
      $unpacked = unpack("H16", $data);
      if (array_pop($unpacked) == '89504e470d0a1a0a') return "png";
      return false;
  }
}

只是提问,我也不确定。为什么不在规则中使用mime请求@Borraciablu我正在通过引用传递一个图像实例,以便调整它的大小。我需要验证我正在发送的对象是否属于该类型,以便我可以在其上调用图像调整大小。在这一点上,它没有mime类型,因为它是一个本机php资源。在这种情况下,为什么不在$image上使用类型暗示呢?如果这是我唯一想检查的东西,那就可以了,但我希望最终有一个更精细的控制级别,然后只是一个需要验证程序的image对象。我的示例已经简化,但它也可以检查以确保要调整大小的图像具有特定的维度和类型。这段代码按原样工作,我只是想把它清理干净。我相信有一个装饰器模式,但我不清楚它是如何实现的。我真的不想使用内置的包。你可以在这里简单地学习Decorator模式:@whoacowboy它不是Decorator。这是一种简单的注射方法。它将表单请求对象注入控制器方法,该方法将在控制器代码处理请求之前自动验证请求;至于图像处理软件包,我强烈推荐:谢谢你的回答。无论名称(命令或作业)如何,我都在寻找验证此命令中发送的数据的最佳方法。写完这篇文章后,我意识到一张图片可能不是案例研究。我会看看马特的帖子,看看它是如何工作的。我想这是你想要的。