Version

Documentation » Media

2. Helpers »

« Media Bundle

1. Installation

1.1. Base bundles

This bundle is mainly dependant of:

This bundle has optional dependencies of:

So be sure you have installed those bundles before starting

1.2. Installation

Retrieve the bundle with composer:

$ composer require sonata-project/media-bundle
$ composer require sonata-project/classification-bundle # (optional: if you need media classification)

Register these bundles in your bundles.php file:

<?php

// config/bundles.php

return [
    //...
    Sonata\MediaBundle\SonataMediaBundle::class => ['all' => true],
    Sonata\EasyExtendsBundle\SonataEasyExtendsBundle::class => ['all' => true],
    JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
];

Note

If you are not using Symfony Flex, you should enable bundles in your AppKernel.php.

<?php
// app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new Sonata\MediaBundle\SonataMediaBundle(),
        new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),

        // You need to add this dependency to make media functional
        new JMS\SerializerBundle\JMSSerializerBundle(),
        // ...
    );
}

Next, add the correct routing files:

  • YAML
    # config/routes.yaml
    
    gallery:
        resource: '@SonataMediaBundle/Resources/config/routing/gallery.xml'
        prefix: /media/gallery
    
    media:
        resource: '@SonataMediaBundle/Resources/config/routing/media.xml'
        prefix: /media
    

Note

If you are not using Symfony Flex, routes should be added to app/config/routing.yml.

Then, you must configure the interaction with the persistence backend you picked:

If you picked Doctrine ORM:

  • YAML
    # config/packages/doctrine.yaml
    
    doctrine:
        orm:
            entity_managers:
                default:
                    mappings:
                        SonataMediaBundle: ~
    
        dbal:
            types:
                json: Sonata\Doctrine\Types\JsonType
    

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

If you picked Doctrine PHPCR:

  • YAML
    # config/packages/doctrine.yaml
    
    doctrine_phpcr:
        odm:
            auto_mapping: true
            mappings:
                SonataMediaBundle:
                    prefix: Sonata\MediaBundle\PHPCR
    

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

Once you have done that, you can configure the Media bundle itself:

  • YAML
    # config/packages/sonata.yaml
    
    sonata_media:
        # if you don't use default namespace configuration
        #class:
        #    media:        MyVendor\MediaBundle\Entity\Media
        #    gallery:      MyVendor\MediaBundle\Entity\Gallery
        #    gallery_item: MyVendor\MediaBundle\Entity\GalleryItem
        db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
        default_context: default # you need to set a context
        contexts:
            default:  # the default context is mandatory
                providers:
                    - sonata.media.provider.dailymotion
                    - sonata.media.provider.youtube
                    - sonata.media.provider.image
                    - sonata.media.provider.file
                    - sonata.media.provider.vimeo
    
                formats:
                    small: { width: 100 , quality: 70}
                    big:   { width: 500 , quality: 70}
    
        cdn:
            server:
                path: /uploads/media # http://media.sonata-project.org/
    
        filesystem:
            local:
                directory:  "%kernel.root_dir%/../public/uploads/media" #flex
                #directory:  "%kernel.root_dir%/../web/uploads/media" #non-flex
                create:     false
    

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

Note

You can define formats per provider type. You might want to set a transversal admin format to be used by the mediaadmin class.

Also, you can determine the resizer to use; the default value is sonata.media.resizer.simple but you can change it to sonata.media.resizer.square

  • YAML
    # config/packages/sonata.yaml
    
    sonata_media:
        providers:
            image:
                resizer: sonata.media.resizer.square
    

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

Note

The square resizer works like the simple resizer when the image format has only the width. But if you specify the height the resizer crop the image in the lower size.

At this point, the bundle is not yet ready. You need to generate the correct entities for the media:

bin/console sonata:easy-extends:generate --dest=src SonataMediaBundle --namespace_prefix=App

Note

If you are not using Symfony Flex, use command without --namespace_prefix=App.

Note

To be able to generate domain objects, you need to have a database driver configure in your project. If it’s not the case, just follow this: http://symfony.com/doc/current/book/doctrine.html#configuring-the-database

Note

The command will generate domain objects in an App\Application namespace. So you can point entities’ associations to a global and common namespace. This will make Entities sharing very easier as your models will allow to point to a global namespace. For instance the media will be App\Application\Sonata\MediaBundle\Entity\Media.

Note

If you are not using Symfony Flex, the namespace will be Application\Sonata\MediaBundle\Entity\Media.

Now, add the new Application Bundle into the bundles.php:

<?php

// config/bundles.php

return [
    //...
    App\Application\Sonata\MediaBundle\ApplicationSonataMediaBundle::class => ['all' => true],
];

Note

If you are not using Symfony Flex, add the new Application Bundle into your AppKernel.php.

<?php
// app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new Application\Sonata\MediaBundle\ApplicationSonataMediaBundle(),
        // ...
    );
}

Configure SonataMediaBundle to use the newly generated classes:

Note

If you are not using Symfony Flex, add classes without the App\ part.

  • YAML
    # config/packages/sonata.yaml
    
    sonata_media:
        # if you don't use default namespace configuration
        class:
            media: App\Application\Sonata\MediaBundle\Entity\Media
            gallery: App\Application\Sonata\MediaBundle\Entity\Gallery
            gallery_has_media: App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia
    
        #...
    

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

If you are not using auto-mapping in doctrine you will have to add it there too:

  • YAML
    # config/packages/doctrine.yaml
    
    doctrine:
        orm:
            entity_managers:
                default:
                    mappings:
                        ApplicationSonataMediaBundle: ~
                        SonataMediaBundle: ~
    

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

You will have to exclude your Application folder from Symfony service autowiring:

  • YAML
    # config/services.yaml
    services:
        App\:
            resource: '../src/*'
            exclude: '../src/{Entity,Tests,Application}'
    

Note

If you are not using Symfony Flex, skip this part.

Now, you can build up your database:

$ bin/console doctrine:schema:[create|update]

If they are not already created, you need to add specific folder to allow uploads from users, make sure your http user can write to this directory:

$ mkdir -p public/uploads/media

Note

If you are not using Symfony Flex, replace public with web folder.

Then you can visit your admin dashboard on http://my-server/admin/dashboard

Found a typo? Something is wrong in this documentation? Just fork and edit it!