nacholibre/richuploader-bundle

安装: 69

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

公开问题: 0

类型:symfony-bundle

0.1.11 2016-11-28 09:10 UTC

This package is not auto-updated.

Last update: 2024-09-26 02:03:45 UTC


README

安装

步骤 1 - 安装必需的 composer 模块

composer require nacholibre/richuploader-bundle
composer require liip/imagine-bundle

liip/imagine-bundle 用于创建上传图片的缩略图。

步骤 1.1 - 资产

确保你在想要使用上传器的页面中添加了 jQuery 和 jQueryUI。

步骤 2 - 在 AppKernel.php 中添加模块

<?php
// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Liip\ImagineBundle\LiipImagineBundle(),
            new nacholibre\RichUploaderBundle\nacholibreRichUploaderBundle(),
        );

        // ...
    }

    // ...
}

步骤 3 - 注册 bundle 路由

# app/config/routing.yml

nacholibre.rich_uploader:
    resource: "@nacholibreRichUploaderBundle/Controller/"
    type:     annotation

步骤 4 - 配置你的映射

# app/config/config.yml

nacholibre_rich_uploader:
    mappings:
        default:
            uri_prefix:         /uploads/richuploader
            upload_destination: %kernel.root_dir%/../web/uploads/richuploader/
            #mime_types: ['image/*']
            max_size: 5M

步骤 4.1 - 安装静态资产

php app/console assets:install --symlink --relativephp bin/console 适用于 3.0 版本。

步骤 5 - 创建你的实体

你需要扩展 RichFile 实体。

<?php
// src/AppBundle/Entity/RichFile.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use nacholibre\RichUploaderBundle\Model\RichFile as nacholibreRichFile;
use nacholibre\RichUploaderBundle\Annotation\RichUploader;

/**
 * @ORM\Entity
 * @ORM\Table(name="images")
 * @RichUploader(config="default")
 */
class RichFile extends nacholibreRichFile {
}

并运行 php app/console doctrine:schema:update --force --dump-sqlphp bin/console .. 如果你使用的是 symfony > 2.8 版本。

步骤 6 - 创建 doctrine 关联

假设你有一个产品实体,并且你想要在其中添加图片。

<?php
// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table(name="product")
 */
class Product {
    //..
    
    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\RichFile")
     * @ORM\JoinTable(name="product_images",
     *      joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id", unique=true)}
     *      )
     * @ORM\OrderBy({"position" = "ASC"})
     */
    private $images;

    /**
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\RichFile")
     */
    private $photo;
    
    //..
}

images 是多个图片/文件上传的示例,而 photo 是单个图片/文件上传的示例。

步骤 7 - 将 RichUploaderType 添加到你的表单类型中

<?php
// src/AppBundle/Form/ProductType.php
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class ProductType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('price')
            ->add('images', 'nacholibre\RichUploaderBundle\Form\Type\RichUploaderType', [
                'entity_class' => 'AppBundle\Entity\RichFile',
                'required' => true, 
                'multiple' => true,
                'size' => 'xs', //available options md and xs
            ])
            ->add('photo', 'nacholibre\RichUploaderBundle\Form\Type\RichUploaderType', [
                'entity_class' => 'AppBundle\Entity\RichFile',
                'required' => true,
                'multiple' => false, //false for single files and true for multiple
                'size' => 'xs', //available options md and xs
            ])
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Product'
        ));
    }
}

为了获取实际文件的链接,使用

nacholibre_rich_uploader_src(fileEntity, 'default')