【问题标题】:Symfony dustin10/VichUploaderBundle eventsSymfonydustin10/VichUploaderBundle 事件
【发布时间】:2015-10-13 13:40:32
【问题描述】:

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

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

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

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

计划是在上传后使用 ImageBundle 调整图像大小。

S

【问题讨论】:

    标签: symfony events vichuploaderbundle


    【解决方案1】:

    你不能“触发”事件,它已经被触发了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 }
    

    您的侦听器类将为 vich uploader 事件键入提示,如下所示:

    // 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多