Version

Documentation » News

3. Advanced Configuration »

« 1. Introduction

2. Installation

2.1. Download the Bundle(s)

composer require sonata-project/news-bundle
composer require sonata-project/doctrine-orm-admin-bundle

If you want to use the API, you also need friendsofsymfony/rest-bundle and nelmio/api-doc-bundle.

composer require nelmio/api-doc-bundle

composer require friendsofsymfony/rest-bundle

2.2. Enable the Bundle(s)

Then, enable the bundle by adding it to the list of registered bundles in bundles.php file of your project:

// config/bundles.php

return [
    // ...
    Sonata\CoreBundle\SonataCoreBundle::class => ['all' => true],
    Ivory\CKEditorBundle\IvoryCKEditorBundle::class => ['all' => true],
    Sonata\NewsBundle\SonataNewsBundle::class => ['all' => true],
    Sonata\BlockBundle\SonataBlockBundle::class => ['all' => true],
    Sonata\UserBundle\SonataUserBundle::class => ['all' => true],
    Sonata\MediaBundle\SonataMediaBundle::class => ['all' => true],
    Sonata\AdminBundle\SonataAdminBundle::class => ['all' => true],
    Sonata\IntlBundle\SonataIntlBundle::class => ['all' => true],
    Sonata\FormatterBundle\SonataFormatterBundle::class => ['all' => true],
    Sonata\ClassificationBundle\SonataClassificationBundle::class => ['all' => true],
    FOS\UserBundle\FOSUserBundle::class => ['all' => true],
    Knp\Bundle\MarkdownBundle\KnpMarkdownBundle::class => ['all' => true],
    Knp\Bundle\MenuBundle\KnpMenuBundle::class => ['all' => true],
    Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle::class => ['all' => true],
    JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
    Sonata\TranslationBundle\SonataTranslationBundle::class => ['all' => true],
    Sonata\EasyExtendsBundle\SonataEasyExtendsBundle::class => ['dev' => true],
];

2.3. Default configuration

# config/packages/sonata_news.yaml

sonata_news:
    title:        Sonata Project
    link:         https://sonata-project.org
    description:  Cool bundles on top of Symfony2
    salt:         'secureToken'
    permalink_generator: sonata.news.permalink.date # sonata.news.permalink.collection

    comment:
        notification:
            emails:   [[email protected], [email protected]]
            from:     [email protected]
            template: '@SonataNews/Mail/comment_notification.txt.twig'
# config/packages/doctrine.yaml

doctrine:
    orm:
        entity_managers:
            default:
                #metadata_cache_driver: apc
                #query_cache_driver: apc
                #result_cache_driver: apc
                mappings:
                    #ApplicationSonataNewsBundle: ~
                    SonataNewsBundle: ~
# config/packages/sonata_media.yaml

news:
    providers:
        - sonata.media.provider.dailymotion
        - sonata.media.provider.youtube
        - sonata.media.provider.image

    formats:
        small: { width: 150 , quality: 95}
        big:   { width: 500 , quality: 90}
  • Create configuration file sonata_formatter.yaml the text formatters available for your blog post:
# config/packages/sonata_formatter.yaml

sonata_formatter:
    formatters:
        markdown:
            service: sonata.formatter.text.markdown
            extensions:
                - sonata.formatter.twig.control_flow
                - sonata.formatter.twig.gist
                - sonata.media.formatter.twig

        text:
            service: sonata.formatter.text.text
            extensions:
                - sonata.formatter.twig.control_flow
                - sonata.formatter.twig.gist
                - sonata.media.formatter.twig

        rawhtml:
            service: sonata.formatter.text.raw
            extensions:
                - sonata.formatter.twig.control_flow
                - sonata.formatter.twig.gist
                - sonata.media.formatter.twig

        richhtml:
            service: sonata.formatter.text.raw
            extensions:
                - sonata.formatter.twig.control_flow
                - sonata.formatter.twig.gist
                - sonata.media.formatter.twig

2.4. Generate the application bundles

bin/console sonata:easy-extends:generate SonataNewsBundle -d src
bin/console sonata:easy-extends:generate SonataUserBundle -d src
bin/console sonata:easy-extends:generate SonataMediaBundle -d src
bin/console sonata:easy-extends:generate SonataClassificationBundle -d src

2.5. Enable the application bundles

// config/bundles.php

return [
    // ...
    App\Application\Sonata\NewsBundle\ApplicationSonataNewsBundle::class => ['all' => true],
    App\Application\Sonata\UserBundle\ApplicationSonataUserBundle::class => ['all' => true],
    App\Application\Sonata\MediaBundle\ApplicationSonataMediaBundle::class => ['all' => true],
    App\Application\Sonata\ClassificationBundle\ApplicationSonataClassificationBundle::class => ['all' => true],
];

2.6. Uncomment the ApplicationSonataNewsBundle mapping

# config/packages/sonata_news.yaml

doctrine:
    orm:
        entity_managers:
            default:
                # ...
                mappings:
                    ApplicationSonataNewsBundle: ~
                    SonataNewsBundle: ~

2.7. Update Database Schema

bin/console doctrine:schema:update --force
  • Complete the FOS/UserBundle install and use the App\Application\Sonata\UserBundle\Entity\User as the user class

2.8. Add SonataNewsBundle routes

# config/packages/routes.yaml

news:
    resource: '@SonataNewsBundle/Resources/config/routing/news.xml'
    prefix: /news

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