Tôi đang sử dụng Laravel 5's Command Bus và tôi chưa rõ cách triển khai lớp trình xác thực.Tôi đang sử dụng Bus lệnh của Laravel 5 và tôi chưa rõ cách triển khai lớp trình xác nhận
Tôi muốn tạo lớp ResizeImageCommandValidator để kiểm tra xem hình ảnh có thực sự là hình ảnh hay không trước khi cố gắng thay đổi kích thước hình ảnh.
Mã tôi muốn rút ra ở đây là phương thức thay đổi kích thước ResizeImageCommandHandler.
if (!($image instanceof Image))
{
throw new ImageInvalidException('ResizeImageCommandHandler');
}
Ý tưởng đến từ Laracasts Commands and Domain Events, nhưng Jeffrey không sử dụng kiến trúc Laravel 5.
Đây là mã.
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;
}
}
chỉ đặt câu hỏi, tôi cũng không chắc chắn. Tại sao không sử dụng Yêu cầu bằng cách sử dụng quy tắc trong quy tắc? http://laravel.com/docs/5.0/validation#rule-mimes – borracciaBlu
@borracciaBlu Tôi đang truyền trong một trường hợp của Hình ảnh bằng cách tham khảo để tôi có thể thay đổi kích thước nó. Tôi cần xác nhận rằng đối tượng mà tôi đang gửi thuộc loại đó để tôi có thể gọi kích thước hình ảnh trên đối tượng đó. Tại thời điểm này nó không có một loại mime vì nó là một nguồn tài nguyên php bản địa. – whoacowboy
Trong trường hợp đó, tại sao không sử dụng Loại Gợi ý trên hình ảnh $? – borracciaBlu