kematjaya/download-bundle

1.3.1 2020-08-01 15:24 UTC

This package is auto-updated.

Last update: 2024-09-29 05:48:48 UTC


README

  1. 安装

    composer require kematjaya/download-bundle
    
  2. 更新 config/bundles.php 配置文件

    Kematjaya\DownloadBundle\DownloadBundle::class => ['all' => true]
    
  3. 创建文件 config/packages/kmj_download.yml

    download:
     upload_dir: '%kernel.project_dir%/public/uploads'
     encrypt: 
         key: ~ # encrypt key
    
  4. 导入路由配置 routes/annotations.yaml

    kmj_download:
      resource: '@DownloadBundle/Resources/config/routing.xml'
    
  5. 使用 twig 模板 config/packages/twig.yaml

    twig:
      form_themes: [
        '@Download/upload_theme.html.twig'
      ]
    
  6. 实现到实体,例如 App\Entity\Person

    namespace App\Entity;
    
    use Kematjaya\DownloadBundle\Entity\DownloadInterface;
    
    /**
    * @ORM\Table(name="person")
    */
    class Person implements DownloadInterface
    {
    
      /**
       * @ORM\Column(type="string", length=255, nullable=true)
       */
      private $images;
      
      public function getImages(): ?string
      {
          return $this->images;
      }
    
      public function setImages(?string $images): self
      {
          $this->images = $images;
    
          return $this;
      }
      
      public static function getPaths():array
      {
          return [
              'getImages' => 'person'  
              // 'getImages' is function where contain file name, and 'person' is path where file uploaded inside 'upload_dir' (part 3)
          ];
      }
    }
    
  7. 别忘了在表单类中添加上传文件类型,例如 App\Form\PersonType

    namespace App\Form;
    
    use App\Entity\Person;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type\FileType;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    
    class PersonType extends AbstractType
    {
      
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
        $builder->add('images', FileType::class);
      }
      
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver->setDefaults([
              'data_class' => Person::class,
          ]);
      }
    }