app-verk / media-bundle
AppVerk媒体包
3.2
2019-09-19 11:36 UTC
Requires
- php: ^7.0
- app-verk/components: ^2.0
README
Symfony媒体包。该包允许以简单的方式上传文件。该包需要配合dropzone.js脚本工作。
配置
使用composer安装包
$ composer require app-verk/media-bundle
在kernel中启用包
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new AppVerk\MediaBundle\MediaBundle(),
// ...
);
}
创建你的媒体类
<?php
namespace AppBundle\Entity;
use AppVerk\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 AppVerk\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许可证下发布。