Seo usage

The SEO manager currently supports 3 level of details in defining the generated SEO data.

These are from weakest to strongest:

global level

This is available by default, can be configured in the admin

module level

These come from custom SEO modules.

At this level you can use placeholders, that will be replaced by actual values during data gathering.

resource level

These come from custom SEO modules.

Each level is capable of defining parts of the generated SEO data. Filling the data works the following way:

  • Check resource level and fill the available parts.
  • If there are unfilled fields left then check module level and fill the available parts with placeholders replaced by actual values.
  • If there are unfilled fields left then check global level and fill the available parts.

Having SEO data in your pages

You have to call the SeoHeaders twig function in your base twig template head section.

# base.html.twig
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        {{ seoHeaders() | raw }}
        ...

Creating custom SEO modules

Creating custom SEO modules allows you to define module and resource level SEO data.

1. Creating your service

You have to create (or extend) a service that implements Loginet\SeoBundle\Model\SeoProvider\SeoProviderInterface

# Loginet\DemoBundle\Model\DemoSeoModule\DemoSeoResource.php
<?php
namespace Loginet\DemoBundle\Model\DemoSeoModule;

use Loginet\SeoBundle\Model\SeoProvider\SeoProviderInterface;

class DemoSeoResource implements SeoProviderInterface
{
    public function getSeoPlaceholderData($presentation)
    {
        return [
            'title' => $presentation->getTitle(),
            'lead' => $presentation->getLead(),
        ];
    }
    public function getSeoPlaceholders()
    {
        return [
            'title',
            'lead',
        ];
    }
}

This interface provides a common way of handling placeholders and replacing their values.

  • getSeoPlaceholders should return an array containing your placeholder tokens as values.
  • getSeoPlaceholderData should return an array containing your placeholder tokens as keys and their values filled in some way based on the provided $presentation

$presentation will be the object you have to pass as 4. argument to setSeo.

2. Tagging the service

You have to tag the service created in step 1. with loginet.seo.provider

# Loginet\DemoBundle\Resources\config\services.yml
services:
    loginet.demo.seo_resource:
        class: Loginet\DemoBundle\Model\DemoSeoModule\DemoSeoResource
        arguments: []
        tags:
            - {name: loginet.seo.provider, alias: demoResource }

You must define a unique alias for your SEO module, you will be using it later. Tagging the service will make the module level editor automatically available in the admin.

3. Adding resource level editor to the admin

You have to use the seoEditor directive in your resource edit page.

# demoResource-edit.html
    ...
    <seo-editor seo-content="data" seo-module="demoResource" locale="hu"></seo-editor>
    ...
  • seo-content argument: An object variable in your current $scope, MUST HAVE an id field. The id field will be used to identify your resource.
  • seo-module argument: Your module alias
  • locale argument: Locale of the edited content

When you save your content you must make a content-saved broadcast. This will cause the seo editor to save your data.

# demoResourceController.js
$scope.save = function(){
    // Save your resource
    $scope.$broadcast('content-saved');
}

4. Modifying your controllers

To actually have your custom data affect the generated SEO headers, you have to call the setSeo method of the SEO manager service loginet.seo.manager.

# Loginet\DemoBundle\Controller\DemoResourceController.php
<?php
namespace Loginet\DemoBundle\Controller;

use Loginet\SeoBundle\Model\Seo\SeoManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DemoResourceController extends Controller
{

    function showDemoResourceAction(Request $request)
    {
        // ... Get your resource in some way
        $sm = $this->getSeoManager();
        $sm->setSeo($request->getLocale(), 'demoResource', $resourceId, $resourceObj);
        // ...
    }

    /**
     * @return SeoManager
     */
    protected function getSeoManager()
    {
        return $this->get('loginet.seo.manager');
    }
}

setSeo arguments:

  1. Locale
  2. Alias of your custom module
  3. Id of your resource
  4. Some kind of representation of your resource