idci / simple-media-bundle
Symfony SimpleMediaBundle
v2.1.0
2013-04-16 12:33 UTC
Requires
- php: >=5.3.2
- doctrine/doctrine-bundle: *
- symfony/framework-bundle: 2.1.*
- twig/twig: *
Suggests
This package is auto-updated.
Last update: 2024-09-24 20:26:23 UTC
README
Symfony2 简单媒体包
安装
要安装此包,请按照以下步骤操作
首先将依赖项添加到您的 composer.json
文件中
"require": { ... "idci/simple-media-bundle": "dev-master" },
然后使用以下命令安装包
php composer update
在您的应用程序内核中启用包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new IDCI\Bundle\SimpleMediaBundle\IDCISimpleMediaBundle(), ); }
在您的 config.yml
文件中包含资源
imports: .... - { resource: @IDCISimpleMediaBundle/Resources/config/config.yml }
现在包已安装。
在 app/config/parameters.yml
中配置您的数据库参数,然后运行
php app/console doctrine:schema:update --force
如何创建媒体
要将媒体与对象关联,请简单实现 MediaAssociableInterface
<?php ... use IDCI\Bundle\SimpleMediaBundle\Entity\MediaAssociableInterface; /** * Object */ class MyObject implements MediaAssociableInterface { ...
然后当您希望上传和关联媒体时,只需调用 idci_simplemedia.manager
服务以创建表单并按以下说明进行处理
// This classic form creation // $form = $this->createForm(new MyObjectType(), $myObject); // Now work like this: $form = $this->get('idci_simplemedia.manager')->createForm( new MyObjectType(), $myObject, array('provider' => 'file') );
如您所见,第三个参数允许您选择提供者。目前只有文件提供者可使用。
要保存并将媒体与对象关联,请按如下方式调用 processForm
函数
if ($this->getRequest()->isMethod('POST')) { $form->bind($this->getRequest()); if ($form->isValid()) { $myObject = $this->get('idci_simplemedia.manager')->processForm($form); return $this->redirect($this->generateUrl(...)); } }
如何检索和显示媒体
控制器
在以下示例中,$obj
必须是实现了 MediaAssociableInterface
的类的实例。因此,要检索媒体集
// Related to an object $medias = $this->get('idci_simplemedia.manager')->getMedias($obj); // Related to an object filter on tags $medias = $this->get('idci_simplemedia.manager')->getMedias($obj, array('tag1', 'tag2')); // Related to tags $medias = $this->get('idci_simplemedia.manager')->getMedias(null, array('tag1', 'tag2'));
要移除与对象 $obj
关联的所有媒体,请使用通过服务提供的 removeAssociatedMedias
函数,如下所示
$this->get('idci_simplemedia.manager')->removeAssociatedMedias($obj); $this->getDoctrine()->getManager()->flush();
要移除单个媒体
$this->get('idci_simplemedia.manager')->removeMedia($media); $em->flush();
视图
如何在twig模板中显示媒体
<!-- Related to an object --> <ul> {% for media in medias(object) %} <li><img src="{{ media.url }}" /></li> {% endfor %} </ul> <!-- Related to an object filter on tags --> <ul> {% for media in medias(object, ['tag']) %} <li><img src="{{ media.url }}" /></li> {% endfor %} </ul> <!-- Related to tags --> <ul> {% for media in medias(null, ['tag1', 'tag2']) %} <li><img src="{{ media.url }}" /></li> {% endfor %} </ul>
如何检索和显示媒体标签
控制器
在以下示例中,$obj
必须是实现了 MediaAssociableInterface
的类的实例。因此,要检索媒体集
// All tags $tags = $this->get('idci_simplemedia.manager')->getTags(); // Related to an object $tags = $this->get('idci_simplemedia.manager')->getTags($obj);
视图
在twig模板中检索标签
<!-- All --> <ul> {% for tag in medias_tags %} <li>{{ tag }}</li> {% endfor %} </ul> <!-- Related to a media --> <ul> {% for tag in medias_tags(object) %} <li>{{ tag }}</li> {% endfor %} </ul>