软激情/媒体包

SoftPassio 媒体包

安装量: 1,057

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 3

类型:symfony-bundle

2.1 2020-06-26 10:35 UTC

This package is auto-updated.

Last update: 2024-09-18 21:52:19 UTC


README

Symfony 媒体包。此包允许轻松上传文件。该包需要使用 dropzone.js 脚本。

配置

使用 Composer 安装包

$ composer require soft-passio/media-bundle

在内核中启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new SoftPassio\MediaBundle\MediaBundle(),
        // ...
    );
}

创建您的媒体类

<?php

namespace AppBundle\Entity;

use SoftPassio\MediaBundle\Entity\Media as BaseMedia;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Media extends BaseMedia
{

}

添加到 config.yml

twig:
    form:
        resources:
            - 'MediaBundle:form:fields.html.twig'
            
media:
    entities:
        media_class: AppBundle\Entity\Media
    allowed_mime_types: ["image/jpeg", "image/jpg", "image/png", "image/gif", "application/pdf"]

添加到 routing.yml

media:
    resource: '@MediaBundle/Controller/'
    type: annotation

将这些库添加到您的管理面板中

<!--css -->
<link rel="stylesheet" href="{{ asset('bundles/media/css/dropzone.min.css') }}" />

<!-- js -->
<script src="{{ asset('bundles/media/js/dropzone.min.js') }}"></script>

更新数据库模式

$ php app/console doctrine:schema:update --force

媒体表单类型

<?php

use Symfony\Component\Form\AbstractType;
use SoftPassio\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Form\FormBuilderInterface;

class Post extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $formMapper
            ->add('image', MediaType::class)
        ;
    }
}

Twig 辅助函数

渲染媒体

<img src="{{ post.media|media }}" />

验证组

该包允许以不同的方式对每个使用的 MediaType 进行验证。例如,您希望只允许 PDF 文件

您需要在 config.yml 中添加组

media:
    entities:
        media_class: AppBundle\Entity\Media
    allowed_mime_types: ["image/png", "image/gif"]
    max_file_size: 15000000
    groups:
        lorem:
            allowed_mime_types: ["application/pdf"]
            max_file_size: 560000

在 MediaType 中设置组

$formMapper->add('image', MediaType::class, [
    'group' => 'lorem'
]);

许可协议

该包在 MIT 许可协议 下发布。