perfectneeds / media-bundle
媒体包管理图片和文档
Requires
- php: ^7.4 || ^8.0
- ext-gd: *
- perfectneeds/service-bundle: ~2.0
- symfony/framework-bundle: ~4.0|~5.0|~6.0
- 2.0.x-dev
- 2.0.27
- 2.0.26
- 2.0.25
- 2.0.24
- 2.0.23
- 2.0.22
- 2.0.21
- 2.0.20
- 2.0.19
- 2.0.18
- 2.0.17
- 2.0.16
- 2.0.15
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.x-dev
- 1.2.19
- 1.2.18
- 1.2.17
- 1.2.16
- 1.2.15
- 1.2.14
- 1.2.13
- 1.2.12
- 1.2.11
- 1.2.10
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.x-dev
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-master
This package is auto-updated.
Last update: 2024-09-12 10:47:40 UTC
README
先决条件
- Symfony 3.4
- PNServiceBundle
安装
安装是一个快速(我保证!)的9步过程
- 使用composer下载PNMediaBundle
- 在AppKernel中启用Bundle
- 创建你的Image类
- 创建你的Document类
- 创建你的ImageRepository类
- 创建你的DocumentRepository类
- 配置PNMediaBundle
- 导入PNMediaBundle路由
- 更新你的数据库模式
步骤1:使用composer下载PNMediaBundle
使用composer要求bundle
$ composer require perfectneeds/media-bundle "~1.0"
步骤2:在AppKernel中启用Bundle
使用composer要求bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new \PN\ServiceBundle\PNServiceBundle(), new \PN\MediaBundle\PNMediaBundle(), // ... ); }
步骤3:创建你的Image类
该bundle的目标是将某些Image类持久化到数据库中。因此,你的第一个任务是创建应用程序的Image类。这个类可以看起来和表现成你想要的任何样子:添加任何你认为有用的属性或方法。这是你的Image类。
该bundle提供了一些基础类,这些类已经映射了大多数字段,这使得创建你的实体变得更容易。以下是使用方法:
- 扩展基础Image类(如果你使用任何Doctrine变体,则来自Entity文件夹)
- 映射id字段。它必须是受保护的,因为它是从父类继承的。
注意!
当你从bundle提供的映射超类扩展时,不要重新定义其他字段的映射,因为这些字段由bundle提供。
在下面的部分中,你将看到你的Image类应该如何看,具体取决于你如何存储你的帖子(Doctrine ORM)。
备注
文档使用了一个名为MediaBundle的bundle。然而,当然你可以将你的帖子类放置在你想要的任何bundle中。
注意!
如果你在Image类中重写了__construct()方法,请确保调用parent::__construct(),因为基础Image类依赖于它来初始化一些字段。
Doctrine ORM Image类
如果你通过Doctrine ORM持久化你的帖子,那么你的Image类应该位于bundle的Entity命名空间中,并从以下开始
*你可以在该类中添加所有与其他实体的关系
<?php // src/PN/Bundle/MediaBundle/Entity/Image.php namespace PN\Bundle\MediaBundle\Entity; use Doctrine\ORM\Mapping as ORM; // DON'T forget the following use statement!!! use PN\MediaBundle\Entity\Image as BaseImage; use PN\MediaBundle\Model\ImageInterface; use PN\MediaBundle\Model\ImageTrait; /** * @ORM\HasLifecycleCallbacks * @ORM\Table("image") * @ORM\Entity(repositoryClass="PN\MediaBundle\Repository\ImageRepository") */ class Image extends BaseImage implements ImageInterface { use ImageTrait; /** * @ORM\PreRemove */ public function preRemove() { $this->removeUpload(); } // *IMPORTANT* Add this code of you use PNContentBundle /** * @ORM\ManyToMany(targetEntity="\PN\Bundle\ContentBundle\Entity\Post", mappedBy="images") */ protected $posts; /** * Constructor */ public function __construct() { parent::__construct(); $this->posts = new \Doctrine\Common\Collections\ArrayCollection(); // your own logic } // if not use the PNContentBundle use this constructor public function __construct() { parent::__construct(); // your own logic } }
步骤4:创建你的Document类
该bundle的目标是将某些Document类持久化到数据库中。因此,你的第一个任务是创建应用程序的Document类。这个类可以看起来和表现成你想要的任何样子:添加任何你认为有用的属性或方法。这是你的Document类。
该bundle提供了一些基础类,这些类已经映射了大多数字段,这使得创建你的实体变得更容易。以下是使用方法:
- 扩展基础Document类(如果你使用任何Doctrine变体,则来自Entity文件夹)
- 映射id字段。它必须是受保护的,因为它是从父类继承的。
注意!
当你从bundle提供的映射超类扩展时,不要重新定义其他字段的映射,因为这些字段由bundle提供。
在下面的部分中,你将看到你的Document类应该如何看,具体取决于你如何存储你的文档(Doctrine ORM)。
备注
文档使用了一个名为MediaBundle的bundle。然而,当然你可以将你的文档类放置在你想要的任何bundle中。
注意!
如果你在Document类中重写了__construct()方法,请确保调用parent::__construct(),因为基础Document类依赖于它来初始化一些字段。
Doctrine ORM Document类
如果你通过Doctrine ORM持久化你的文档,那么你的Document类应该位于bundle的Entity命名空间中,并从以下开始
*你可以在该类中添加所有与其他实体的关系
<?php // src/PN/Bundle/MediaBundle/Entity/Document.php namespace PN\Bundle\MediaBundle\Entity; use Doctrine\ORM\Mapping as ORM; // DON'T forget the following use statement!!! use PN\MediaBundle\Entity\Document as BaseDocument; use PN\MediaBundle\Model\DocumentInterface; use PN\MediaBundle\Model\DocumentTrait; /** * @ORM\HasLifecycleCallbacks * @ORM\Table("document") * @ORM\Entity(repositoryClass="PN\Bundle\MediaBundle\Repository\DocumentRepository") */ class Document extends BaseDocument implements DocumentInterface { use DocumentTrait; /** * @ORM\PreRemove */ public function preRemove() { $this->removeUpload(); } // if not use the PNContentBundle use this constructor public function __construct() { parent::__construct(); // your own logic } }
步骤 5:创建您的 ImageRepository 类
您可以使用这个 Repository
添加任何自定义方法
<?php // src/PN/Bundle/MediaBundle/Repository/ImageRepository.php namespace PN\Bundle\MediaBundle\Repository; use PN\MediaBundle\Repository\ImageRepository as BaseImageRepository; class ImageRepository extends BaseImageRepository { }
步骤 6:创建您的 DocumentRepository 类
您可以使用这个 Repository
添加任何自定义方法
<?php // src/PN/Bundle/MediaBundle/Repository/DocumentRepository.php namespace PN\Bundle\MediaBundle\Repository; use PN\MediaBundle\Repository\DocumentRepository as BaseDocumentRepository; class DocumentRepository extends BaseDocumentRepository { }
步骤 7:配置 PNMediaBundle
根据您使用的数据存储类型,将以下配置添加到您的 config.yml 文件中。
# app/config/config.yml
doctrine:
orm:
# search for the "ResolveTargetEntityListener" class for an article about this
resolve_target_entities:
PN\MediaBundle\Entity\Image: PN\Bundle\MediaBundle\Entity\Image
PN\MediaBundle\Entity\Document: PN\Bundle\MediaBundle\Entity\Document
pn_media:
image:
# The fully qualified class name (FQCN) of the Image class which you created in Step 3.
image_class: PN\Bundle\MediaBundle\Entity\Image
# All supported mime types for images
mime_types: ['image/gif', 'image/jpeg', 'image/jpg', 'image/png']
# Add here all upload paths for images that not managed by image setting and you'll path this id in upload method
# *IMPORTANT* this id must be greater than or equal 100
upload_paths:
- { id: 100, path: 'banner', width: 500, height: 500, validateWidthAndHeight: true }
- { id: 101, path: 'testimonials', width: auto, height: auto }
- { id: 102, path: 'our-reference' }
document:
#The fully qualified class name (FQCN) of the Document class which you created in Step 4.
document_class: PN\Bundle\MediaBundle\Entity\Document
# All supported mime types for document
mime_types: ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/mspowerpoint', 'application/powerpoint', 'application/vnd.ms-powerpoint', 'application/x-mspowerpoint', 'application/pdf', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
# Add here all upload paths for documents and you'll path this id in upload method
# *IMPORTANT* this id must be greater than 100
upload_paths:
- { id: 100, path: 'application' }
步骤 8:导入 PNMediaBundle 路由文件
# app/config/routing.yml
pn_media:
resource: "@PNMediaBundle/Resources/config/routing.yml"
步骤 9:更新您的数据库模式
现在,配置了 bundle 之后,您需要做的最后一件事是更新数据库模式,因为您添加了一个新的实体。
$ php bin/console doctrine:schema:update --force
如何使用 PNMediaBundle
- 如何在控制器中上传 图像
- 如何在控制器中上传 文档
1. 如何在控制器中上传 图像
<?php $file = $form->get("image")->get("file")->getData(); $this->get('pn_media_upload_image')->uploadSingleImage($entity, $file, 100, $request);
- $entity : 您想添加此图像的实体的实例 此实体必须包含 addImage() 或 setImage() 中的一个方法
- $file: 必须是 FileUploader 的实例
- $type: 设置上传路径的实体类型,此类型必须在
ImageSetting
中找到或在app/config.yml
中配置 - $request (可选) : 一个 Symfony\Component\HttpFoundation\Request 的实例
- $imageType (默认值:主图像):任何图像类型
2. 如何在控制器中上传 文档
<?php $file = $form->get("document")->get("file")->getData(); $this->get('pn_media_upload_document')->uploadSingleDocument($entity, $file, 100, $request);
- $entity : 您想添加此文档的实体的实例 此实体必须包含 addDocument() 或 setDocument() 中的一个方法
- $file: 必须是 FileUploader 的实例
- $type: 设置上传路径的实体类型,此类型必须在
app/config.yml
中配置 - $request (可选) : 一个 Symfony\Component\HttpFoundation\Request 的实例
报告问题或功能请求
问题和功能请求在 Github 问题跟踪器 中跟踪。
当报告一个错误时,在 Symfony 标准版 中重现它可能是个好主意,这样 bundle 的开发者可以通过简单地克隆它并遵循一些步骤来重现问题。