The SEO manager currently supports 3 level of details in defining the generated SEO data.
These are from weakest to strongest:
global
levelThis is available by default, can be configured in the admin
module
levelThese come from custom SEO modules.
At this level you can use placeholders, that will be replaced by actual values during data gathering.
resource
levelThese come from custom SEO modules.
Each level is capable of defining parts of the generated SEO data. Filling the data works the following way:
resource
level and fill the available parts.module
level and fill the available parts with placeholders replaced by actual values.global
level and fill the available parts.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 allows you to define module
and resource
level SEO data.
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
.
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.
resource
level editor to the adminYou 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 aliaslocale
argument: Locale of the edited contentWhen 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');
}
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: