3. Usage

The bundle provides a shared service accessible by the name sonata.seo.page. The service is an instance of SeoPage which contains methods to alter SEO information for the current request.

By default, the object is initialized with values defined in the configuration:

# config/packages/sonata_seo.yaml

    encoding: 'UTF-8'
        default:   'sonata.seo.page.default'
        separator: ' - '
        title:     'Project name'

However, it is possible to alter these values at runtime:

$post = $this->getPostManager()->findOneByPermalink($permalink, $this->container->get('sonata.news.blog'));

$seoPage = $this->container->get('sonata.seo.page');

    ->addMeta('name', 'description', $post->getAbstract())
    ->addMeta('property', 'og:title', $post->getTitle())
    ->addMeta('property', 'og:type', 'blog')
    ->addMeta('property', 'og:url',  $this->generateUrl('sonata_news_view', [
        'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post, true)
    ], true))
    ->addMeta('property', 'og:description', $post->getAbstract())
    ->setBreadcrumb('news_post', [
        'post' => $post,

You could also prepend the page title, so that the global title is used as a suffix:

$seoPage = $this->container->get('sonata.seo.page');


These values can be used inside a twig template.

<!DOCTYPE html>
<html {{ sonata_seo_html_attributes() }}>
    <head {{ sonata_seo_head_attributes() }}>
        {{ sonata_seo_title() }}
        {{ sonata_seo_metadatas() }}
        {{ sonata_seo_link_canonical() }}
        {{ sonata_seo_lang_alternates() }}