Version

Documentation » Page

3. Getting Started »

« 1. Introduction

2. Installation

2.1. Prerequisites

PHP 7.1 and Symfony 2.8, >=3.3 or 4 are needed to make this bundle work, there are also some Sonata dependencies that need to be installed and configured beforehand:

Note

If a dependency is already installed somewhere in your project or in another dependency, you won’t need to install it again.

2.2. Enable the Bundle

Add the dependant bundles to the vendor/bundles directory:

composer require sonata-project/page-bundle --no-update

# for SonataPageBundle > 2.3.6
composer require sonata-project/datagrid-bundle --no-update
composer require sonata-project/doctrine-orm-admin-bundle --no-update

# optional when using API
composer require friendsofsymfony/rest-bundle  --no-update
composer require nelmio/api-doc-bundle  --no-update

composer update

Next, be sure to enable the bundles in your bundles.php file if they are not already enabled:

// config/bundles.php

return [
    // ...
    Sonata\PageBundle\SonataPageBundle::class => ['all' => true],
    Sonata\EasyExtendsBundle\SonataEasyExtendsBundle::class => ['all' => true],
];

2.3. Configuration

2.3.1. Doctrine Configuration

Add these bundles in the config mapping definition (or enable auto_mapping):

# config/packages/doctrine.yaml

doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    ApplicationSonataPageBundle: ~ # only once the ApplicationSonataPageBundle is generated
                    SonataPageBundle: ~

    dbal:
        types:
            json: Sonata\Doctrine\Types\JsonType

2.3.2. CMF Routing Configuration

sonata.page.router service must be added to the index of cmf_routing.router chain router.

Configure symfony-cmf/routing-bundle:

# config/packages/cmf_routing_bundle.yaml

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

Or register sonata.page.router automatically:

# config/packages/sonata_page.yaml

sonata_page:
    router_auto_register:
        enabled: true
        priority: 150

2.3.3. SonataPageBundle Configuration

# config/packages/sonata_page.yaml

sonata_page:
    slugify_service:   sonata.core.slugify.cocur # old BC value is sonata.core.slugify.native
    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_cache_esi
        - sonata_page_cache_ssi
        - 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: '@SonataPage/layout.html.twig',          name: 'default' }
        2columns: { path: '@SonataPage/2columns_layout.html.twig', name: '2 columns layout' }

    direct_publication: false # or %kernel.debug% if you want to publish in dev mode (but not in prod)

    # 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

2.3.4. SonataAdminBundle Configuration

# config/packages/sonata_admin.yaml

sonata_admin:
    assets:
        extra_javascripts:
            - bundles/sonatapage/sonata-page.back.min.js
        extra_stylesheets:
            - bundles/sonatapage/sonata-page.back.min.css

2.3.5. SonataBlockBundle Configuration

# config/packages/sonata_block.yaml

sonata_block:
    context_manager: sonata.page.block.context_manager

Note

Please you need to use the context sonata_page_bundle in the SonataBlockBundle to add block into a Page.

2.3.6. Security Configuration

# config/packages/security.yaml

security:
    role_hierarchy:
        ROLE_ADMIN: ROLE_USER
        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

If you have decided to customize your logout management (in particular if you have set invalidate_session to false), you might want to add this logout handler:

# config/packages/security.yaml

security:
    firewalls:
        main: # replace with your firewall name
            logout:
                handlers: ['sonata.page.cms_manager_selector']

2.3.7. Routing Configuration

# config/routes.yaml

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

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

2.4. Extend the Bundle

At this point, the bundle is usable, but not quite ready yet. You need to generate the correct entities for the page:

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

With provided parameters, the files are generated in src/Application/Sonata/PageBundle.

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 easier as your models will allow to point to a global namespace. For instance the page will be App\Application\Sonata\PageBundle\Entity\Page.

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

// config/bundles.php

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

Configure SonataPageBundle to use the newly generated classes:

# config/packages/sonata_page.yaml

sonata_page:
    class:
        page: App\Application\Sonata\PageBundle\Entity\Page # This is an optional value
        snapshot: App\Application\Sonata\PageBundle\Entity\Snapshot
        block: App\Application\Sonata\PageBundle\Entity\Block
        site: App\Application\Sonata\PageBundle\Entity\Site

The only thing left is to update your schema:

bin/console doctrine:schema:update --force

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