Symfony dustin10/VichUploaderBundle事件

use*_*736 3 events symfony vichuploaderbundle

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

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

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

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

计划是使用Post Upload上的ImageBundle调整图像大小.

小号

jcr*_*oll 5

你不能"触发"这个事件,它已经在这里被触发:

   /**
     * 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));
    }
Run Code Online (Sandbox Code Playgroud)

你可以做的是处理的事件,这是我想你指的是.您可以通过创建此处概述的侦听器来实现此目的.听众会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 }
Run Code Online (Sandbox Code Playgroud)

监听器类将typehint像下面的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
    }
}
Run Code Online (Sandbox Code Playgroud)