Symfony dustin10/VichUploaderBundle 事件

Symfony dustin10/VichUploaderBundle events

我正在使用 dustin10/VichUploaderBundle 上传图片。

我正在使用 Gregwar/ImageBundle 调整图像大小。

dustin10/VichUploaderBundle 有一个 POST_UPLOAD 事件。我如何触发事件。我已经阅读了文档,但没有说明如何触发事件。

https://github.com/dustin10/VichUploaderBundle/blob/master/Event/Events.php

计划在 Post 上传时使用 ImageBundle 调整图像大小。

S

你不能"trigger"事件,它已经触发了here:

   /**
     * Checks for file to upload.
     *
     * @param object $obj       The object.
     * @param string $fieldName The name of the field containing the upload (has to be mapped).
     */
    public function upload($obj, $fieldName)
    {
        $mapping = $this->getMapping($obj, $fieldName);
        // nothing to upload
        if (!$this->hasUploadedFile($obj, $mapping)) {
            return;
        }
        $this->dispatch(Events::PRE_UPLOAD, new Event($obj, $mapping));
        $this->storage->upload($obj, $mapping);
        $this->injector->injectFile($obj, $mapping);
        $this->dispatch(Events::POST_UPLOAD, new Event($obj, $mapping));
    }

您可以做的是处理我认为您所指的事件。您可以按照概述 here 创建一个侦听器来做到这一点。侦听器将像这样侦听 POST_UPLOAD 事件:

# app/config/services.yml
services:
    app_bundle.listener.uploaded_file_listener:
        class: AppBundle\EventListener\UploadedFileListener
        tags:
            - { name: kernel.event_listener, event: vich_uploader.post_upload, method: onPostUpload }

您的侦听器 class 将为 vich 上传事件键入提示,如下所示:

// src/AppBundle/EventListener/AcmeRequestListener.php
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\HttpKernel;
use Vich\UploaderBundle\Event\Event;

class UploadedFileListener
{
    public function onPostUpload(Event $event)
    {
        $uploadedFile = $event->getObject();
        // your custom logic here
    }
}