我正在使用 Laravel 5 的命令总线,但我不清楚如何实施验证器 class
I am using Laravel 5's Command Bus and I am unclear how to implement a validator class
我正在使用 Laravel 5's Command Bus,但我不清楚如何实施验证器 class。
我想创建一个 ResizeImageCommandValidator class 以在尝试调整图像大小之前检查该图像是否确实是图像。
我想提取的代码来自 ResizeImageCommandHandler 调整大小方法。
if (!($image instanceof Image))
{
throw new ImageInvalidException('ResizeImageCommandHandler');
}
这个想法来自 Laracasts Commands and Domain Events,但 Jeffrey 没有使用 Laravel 5 架构。
这是代码。
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();
};
}
}
ResizeImageCommand.php
<?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
class 在请求发送到命令总线之前捕获您的请求:
public function postResizeImage(ResizeImageRequest $request) {
// Send request to the Command Bus
}
然后在您的 ResizeImageRequest
class 中放入规则,您可能需要自定义验证来验证上传的文件是否实际上是图像。
你可以使用 https://packagist.org/packages/intervention/image to work with image files. Or you could use this https://github.com/cviebrock/image-validator
问我是否需要进一步的帮助
在尝试回答您的问题时,我建议您不要过于关注其中的 Command 部分。在 Laravel 5.1 中,该文件夹正在重命名为 "Jobs" - 请参阅;
https://laravel-news.com/2015/04/laravel-5-1/
这恰恰是因为泰勒觉得人们对这个词太拘泥了 "Command."
另见 http://www.laravelpodcast.com/episodes/6823-episode-21-commands-pipelines-and-packages and https://laracasts.com/lessons/laravel-5-commands
Illuminate 包中的验证器 class 非常棒,http://laravel.com/api/5.0/Illuminate/Validation/Validator.html - 我想我不确定它有什么问题。
我要说的是,除非您有令人信服的理由为此使用命令 class,否则请不要这样做。另见:http://www.laravelpodcast.com/episodes/9313-episode-23-new-beginnings-envoyer-laravel-5-1
我谦虚地建议你可能问错了问题,也许你不需要使用命令来处理这个问题。
这可能是您正在寻找的答案:https://mattstauffer.co/blog/laravel-5.0-validateswhenresolved
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
如果这不起作用,请注册 Larachat,http://larachat.co/ - 一个专门用于这类事情的 Slack 频道。 Laravel 帮助的最佳去处。 (当然 Stack Overflow 除外)
这里有一些 class 我用它来检查图像的格式,我想你可能会发现它有用。
<?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;
}
}
我正在使用 Laravel 5's Command Bus,但我不清楚如何实施验证器 class。
我想创建一个 ResizeImageCommandValidator class 以在尝试调整图像大小之前检查该图像是否确实是图像。
我想提取的代码来自 ResizeImageCommandHandler 调整大小方法。
if (!($image instanceof Image))
{
throw new ImageInvalidException('ResizeImageCommandHandler');
}
这个想法来自 Laracasts Commands and Domain Events,但 Jeffrey 没有使用 Laravel 5 架构。
这是代码。
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();
};
}
}
ResizeImageCommand.php
<?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
class 在请求发送到命令总线之前捕获您的请求:
public function postResizeImage(ResizeImageRequest $request) {
// Send request to the Command Bus
}
然后在您的 ResizeImageRequest
class 中放入规则,您可能需要自定义验证来验证上传的文件是否实际上是图像。
你可以使用 https://packagist.org/packages/intervention/image to work with image files. Or you could use this https://github.com/cviebrock/image-validator
问我是否需要进一步的帮助
在尝试回答您的问题时,我建议您不要过于关注其中的 Command 部分。在 Laravel 5.1 中,该文件夹正在重命名为 "Jobs" - 请参阅;
https://laravel-news.com/2015/04/laravel-5-1/
这恰恰是因为泰勒觉得人们对这个词太拘泥了 "Command."
另见 http://www.laravelpodcast.com/episodes/6823-episode-21-commands-pipelines-and-packages and https://laracasts.com/lessons/laravel-5-commands
Illuminate 包中的验证器 class 非常棒,http://laravel.com/api/5.0/Illuminate/Validation/Validator.html - 我想我不确定它有什么问题。
我要说的是,除非您有令人信服的理由为此使用命令 class,否则请不要这样做。另见:http://www.laravelpodcast.com/episodes/9313-episode-23-new-beginnings-envoyer-laravel-5-1
我谦虚地建议你可能问错了问题,也许你不需要使用命令来处理这个问题。
这可能是您正在寻找的答案:https://mattstauffer.co/blog/laravel-5.0-validateswhenresolved
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
如果这不起作用,请注册 Larachat,http://larachat.co/ - 一个专门用于这类事情的 Slack 频道。 Laravel 帮助的最佳去处。 (当然 Stack Overflow 除外)
这里有一些 class 我用它来检查图像的格式,我想你可能会发现它有用。
<?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;
}
}