Installation du CMS Sonata Page

Voici comment installer le CMS Sonata Page à partir d’une installation neuve de Symfony. Cet article sera modifié régulièrement au fur et à mesure de mes découvertes.

Le but est de mettre en place un environnement complet à savoir:

  • Un système de CMS nous permettant de créer des pages, modifier des blocks dans les pages sans avoir à éditer de code
  • Une gestion de membres, au moins pour protéger l’administration dans un premier temps
  • Une gestion des médias permettant l’insertion d’images, vidéos etc.
  • La possibilité d’utiliser de l' »HTML Riche » pour les publications
  • Une administration permettant de gérer tout ce petit monde.

Installation de Symfony 2 via composer :

php composer.phar create-project symfony/framework-standard-edition sonata/ 2.2.1

Modifier le composer.json pour y inclure les bundles suivants :

"sonata-project/page-bundle": "2.3.*@dev",
"sonata-project/doctrine-orm-admin-bundle": "2.2.*@dev",
"sonata-project/admin-bundle": "2.2.*@dev",
"sonata-project/block-bundle": "2.2.*@dev",
"knplabs/knp-menu-bundle": "1.1.*@dev",
"sonata-project/seo-bundle": "1.1.1",
"sonata-project/easy-extends-bundle": "2.1.*@dev",
"sonata-project/notification-bundle": "2.2.*@dev",
"guzzle/guzzle": "3.3.*@dev",
"liip/monitor-bundle": "1.0.*@dev",
"liip/monitor": "1.0.*@dev",
"videlalvaro/php-amqplib": "v2.0.2",
"sonata-project/user-bundle": "2.2.*@dev",
"sonata-project/media-bundle": "2.2.*@dev",
"sonata-project/formatter-bundle": "2.2.*@dev"

puis lancer la commande

php composer.phar update

Pour télécharger les mises à jour.

Activer les Bundles suivants :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public function registerBundles()
{
    return array(
        // ...
        new Sonata\CacheBundle\SonataCacheBundle(),
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Sonata\SeoBundle\SonataSeoBundle(),
        new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
        new Sonata\NotificationBundle\SonataNotificationBundle(),
        new Sonata\jQueryBundle\SonatajQueryBundle(),
        new Sonata\AdminBundle\SonataAdminBundle(),
        new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
        new Sonata\PageBundle\SonataPageBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        new Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle,
        new FOS\UserBundle\FOSUserBundle(),
        new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
        new Sonata\MediaBundle\SonataMediaBundle(),
        new Sonata\MarkItUpBundle\SonataMarkItUpBundle(),
        new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle(),
        new Ivory\CKEditorBundle\IvoryCKEditorBundle(),
        new Sonata\FormatterBundle\SonataFormatterBundle(),
        // new Application\Sonata\PageBundle\ApplicationSonataPageBundle(),
        // new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
        // new Application\Sonata\MediaBundle\ApplicationSonataMediaBundle(),
        // ...
    );
}

Pensez bien à laisser les lignes indiquées comme commentées, on les activera plus tard ! Puis dans le config.yml

# Configuration Sonata Cache
sonata_cache:
    caches:
        esi:
            token: an unique security key # a random one is generated by default
            servers:
                - varnishadm -T 127.0.0.1:2000 {{ COMMAND }} "{{ EXPRESSION }}"

        ssi:
            token: an unique security key # a random one is generated by default

        #mongo:
        #    database:   cache
        #    collection: cache
        #    servers:
        #        - {host: 127.0.0.1, port: 27017, user: username, password: pASS'}
        #        - {host: 127.0.0.2}

        #memcached:
        #    prefix: test     # prefix to ensure there is no clash between instances
        #    servers:
        #        - {host: 127.0.0.1, port: 11211, weight: 0}

        #apc:
        #    token:  s3cur3   # token used to clear the related cache
        #    prefix: test     # prefix to ensure there is no clash between instances
        #    servers:
        #        - { domain: kooqit.local, ip: 127.0.0.1, port: 80}

# Configuration Sonata Block
sonata_block:
    default_contexts: [cms]
    context_manager: sonata.page.block.context_manager
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]

        #sonata.admin_doctrine_orm.block.audit:
        #    contexts:   [admin]

        sonata.page.block.children_pages:
        sonata.block.service.text:
        sonata.block.service.rss:
        sonata.formatter.block.formatter:

        # Some specific block from the SonataMediaBundle
        #sonata.media.block.media:
        #sonata.media.block.gallery:
        #sonata.media.block.feature_media:

# Configuration Sonata Seo
sonata_seo:
    default:          sonata.seo.page.default
    encoding:         UTF-8
    page:
        title:            Sonata Project
        metas:
            name:
                keywords:             foo bar
                description:          The description
                robots:               index, follow

            property:
                # Facebook application settings
                #'fb:app_id':          XXXXXX
                #'fb:admins':          admin1, admin2

                # Open Graph information
                # see http://developers.facebook.com/docs/opengraphprotocol/#types or http://ogp.me/
                'og:site_name':       Sonata Project Sandbox
                'og:description':     A demo of the some rich bundles for your Symfony2 projects

            http-equiv:
                'Content-Type':         text/html; charset=utf-8
                #'X-Ua-Compatible':      IE=EmulateIE7

        head:
            'xmlns':              http://www.w3.org/1999/xhtml
            'xmlns:og':           http://opengraphprotocol.org/schema/
            #'xmlns:fb':           "http://www.facebook.com/2008/fbml"

# Configuration Sonata Notification
sonata_notification:
    backend: sonata.notification.backend.runtime

# Symfony CMF
cmf_routing:
    chain:
        routers_by_id:
            # enable the DynamicRouter with high priority to allow overwriting configured routes with content
            #symfony_cmf_routing_extra.dynamic_router: 200
            # enable the symfony default router with a lower priority
            sonata.page.router: 150
            router.default: 100

# Configuration Sonata Pages
sonata_page:
    multisite: host
    use_streamed_response: true # set the value to false in debug mode or if the reverse proxy does not handle streamed response
    ignore_route_patterns:
        - ^(.*)admin(.*)   # ignore admin route, ie route containing 'admin'
        - ^_(.*)          # ignore symfony routes
    ignore_routes:
        - sonata_page_esi_cache
        - sonata_page_ssi_cache
        - sonata_page_js_sync_cache
        - sonata_page_js_async_cache
        - sonata_cache_esi
        - sonata_cache_ssi
        - sonata_cache_js_async
        - sonata_cache_js_sync
        - sonata_cache_apc

    ignore_uri_patterns:
        - ^/admin(.*)   # ignore admin route, ie route containing 'admin'

    page_defaults:
        homepage: {decorate: false} # disable decoration for homepage, key - is a page route
    default_template: default # template key from templates section, used as default for pages
    templates:
        default: {path: 'SonataPageBundle::layout.html.twig', name: default }
    # manage the http errors
    catch_exceptions:
        not_found: [404]    # render 404 page with "not_found" key (name generated: _page_internal_error_{key})
        fatal:     [500]    # so you can use the same page for different http errors or specify specific page for each error

# Configuration KNP Menu
knp_menu:
    twig:  # use "twig: false" to disable the Twig extension and the TwigRenderer
        template: knp_menu.html.twig
    templating: false # if true, enables the helper for PHP templates
    default_renderer: twig # The renderer to use, list is also available by default

sonata_user:
    security_acl: true
    manager_type: orm # can be orm or mongodb

fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Application\Sonata\UserBundle\Entity\User
    group:
        group_class: Application\Sonata\UserBundle\Entity\Group

sonata_media:
    default_context: default
    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr
    contexts:
        default:  # the default context is mandatory
            providers:
                - sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                - sonata.media.provider.file

            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%/../web/uploads/media
            create:     false

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

        twig:
            service: sonata.formatter.text.twigengine
            extensions: [] # Twig formatter cannot have extensions

sonata_admin:
    templates:
        # default global templates
        layout:  SonataAdminBundle::layout.html.twig

Rajouter dans la partie Doctrine :

doctrine:
    dbal:
        # ...
        types:
          json: Sonata\Doctrine\Types\JsonType

et dans la partie framework :

framework:
    # ...
    translator: ~

Enfin dans la partie twig :

twig:
    #...
    form:
        resources:
            - 'SonataAdminBundle:Form:silex_form_div_layout.html.twig'
            - 'SonataFormatterBundle:Form:formatter.html.twig'

Ajouter dans le routing:

gallery:
    resource: '@SonataMediaBundle/Resources/config/routing/gallery.xml'
    prefix: /media/gallery

media:
    resource: '@SonataMediaBundle/Resources/config/routing/media.xml'
    prefix: /media

sonata_page_exceptions:
    resource: '@SonataPageBundle/Resources/config/routing/exceptions.xml'
    prefix: /

sonata_page_cache:
    resource: '@SonataCacheBundle/Resources/config/routing/cache.xml'
    prefix: /

admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

sonata_user:
    resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
    prefix: /admin

Modifier le security.yml comme suit :

security:
    encoders:
        Application\Sonata\UserBundle\Entity\User: sha512

    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH, SONATA]
        SONATA:
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT # if you are not using acl then this line must be uncommented
            - ROLE_SONATA_PAGE_ADMIN_BLOCK_EDIT
    acl:
        connection: default

    providers:
        fos_userbundle:
            id: fos_user.user_manager
    firewalls:
        # -> custom firewall for the admin area of the URL
        admin:
            switch_user:        true
            context:            user
            pattern:            /admin(.*)
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
                use_referer:    true
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:    true
        # -> end custom configuration

        # defaut login area for standard users
        main:
            switch_user:        true
            context:            user
            pattern:            .*
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:             true
            anonymous:          true

    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # -> custom access control for the admin area of the URL
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        # -> end

        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

On va maintenant créer le fichier twig layout.html.twig dans app/Resources/SonataAdminBundle/views/ et y placer ce contenu :

{% extends 'SonataAdminBundle::standard_layout.html.twig' %}

{% block stylesheets %}
    {{ parent() }}

    <link rel="stylesheet" href="{{ asset('bundles/sonatamarkitup/markitup/markitup/skins/sonata/style.css') }}" type="text/css" media="all" />
    <link rel="stylesheet" href="{{ asset('bundles/sonatamarkitup/markitup/markitup/sets/markdown/style.css') }}" type="text/css" media="all" />
    <link rel="stylesheet" href="{{ asset('bundles/sonatamarkitup/markitup/markitup/sets/html/style.css') }}" type="text/css" media="all" />
    <link rel="stylesheet" href="{{ asset('bundles/sonatamarkitup/markitup/markitup/sets/textile/style.css') }}" type="text/css" media="all" />
{% endblock %}

{% block javascripts %}
    {{ parent() }}

    <script src="{{ asset('bundles/ivoryckeditor/ckeditor.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/sonatamarkitup/markitup/markitup/jquery.markitup.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/sonatamarkitup/markitup/markitup/sets/markdown/set.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/sonatamarkitup/markitup/markitup/sets/html/set.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/sonatamarkitup/markitup/markitup/sets/textile/set.js') }}" type="text/javascript"></script>
{% endblock %}

lancer ensuite les commandes suivantes

> php app/console assets:install web
> php app/console sonata:easy-extends:generate --dest=src SonataPageBundle
> php app/console sonata:easy-extends:generate --dest=src SonataUserBundle
> php app/console sonata:easy-extends:generate --dest=src SonataMediaBundle

Maintenant on va activer le SonataPageBundle et le SonataUserBundle en décommentant la ligne dans l’AppKernel.php:

1
2
3
            new Application\Sonata\PageBundle\ApplicationSonataPageBundle(),
            new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
            new Application\Sonata\MediaBundle\ApplicationSonataMediaBundle(),

Pensez à paramétrer votre base de données dans parameters.yml puis videz le cache:

> php app/console cache:clear

Enfin on lance la création des tables dans la base de données :

> php app/console doctrine:schema:update --force

Création du site par défaut

> php app/console sonata:page:create-site

Renseigner les informations telles qu’indiquées :

Please define a value for Site.name : Sonata Test
Please define a value for Site.host : sonata.localhost
Please define a value for Site.relativePath : /
Please define a value for Site.enabled : true
Please define a value for Site.locale : -
Please define a value for Site.enabledFrom : now
Please define a value for Site.enabledTo : +10 years
Please define a value for Site.default : true

Creating website with the following information :
  name : localhost
  site : http(s)://localhost
  enabled :  Tue, 10 Jan 2012 16:12:08 +0100 => Mon, 10 Jan 2022 16:12:08 +0100

Confirm site creation ?y

Site created !

You can now create the related pages and snapshots by running the followings commands:
  php app/console sonata:page:update-core-routes --site=1
  php app/console sonata:page:create-snapshots --site=1

On lance ensuite la mise à jour des pages et des snapshots

> php app/console sonata:page:update-core-routes --site=all
> php app/console sonata:page:create-snapshots --site=all

Maintenant on va créer notre premier utilisateur, l’administrateur:

> php app/console fos:user:create

Il faut renseigner le login de d’administrateur, son adresse email et son mot de passe. Maintenant que notre utilisateur est créé, il faut en faire un super administrateur:

> php app/console fos:user:promote admin --super

Normalement si vous vous connectez sur http://sonata.localhost/app_dev.php/admin/dashboard, vous devriez pouvoir vous connecter et accéder à l’administration !

Dans un prochain article, on passera à l’étape utilisation !

Mises à jour :

  • 14/05/2013 : Ajout du Bundle SonataMedia
  • 24/05/2013 : Ajout du Bundle Formatter
  • 31/05/2013 : Activation du Block Children dans config.yml
  • 17/07/2013: Mise à jour de composer.json, AppKernel.php et config.yml

Mots-clefs : , ,



Laisser une réponse

Vous devez être connecté pour publier un commentaire.