|
| 1 | +Using Extractors to Retrieve the Seo Metadata |
| 2 | +============================================= |
| 3 | + |
| 4 | +Instead of setting every value to the ``SeoMetadata`` manually, an extractor |
| 5 | +can do the work for you. Extractors are executed when the content object |
| 6 | +implements a specific interface. The method required by that interface will |
| 7 | +return the value for the specific SEO data. The extractor will then update the |
| 8 | +``SeoMetadata`` object for the current object with the returned value. |
| 9 | + |
| 10 | +Available Extractors |
| 11 | +-------------------- |
| 12 | + |
| 13 | ++--------------------------------+---------------------------+----------------------------------------------+ |
| 14 | +| ExtractorInterface | Method | Type | |
| 15 | ++================================+===========================+==============================================+ |
| 16 | +| ``DescriptionReadInterface`` | ``getSeoDescription()`` | Returns the meta description | |
| 17 | ++--------------------------------+---------------------------+----------------------------------------------+ |
| 18 | +| ``TitleReadInterface`` | ``getSeoTitle()`` | Returns the page title | |
| 19 | ++--------------------------------+---------------------------+----------------------------------------------+ |
| 20 | +| - | ``getTitle()`` | If the document has a ``getTitle()`` method, | |
| 21 | +| | | it'll be used as the page title | |
| 22 | ++--------------------------------+---------------------------+----------------------------------------------+ |
| 23 | +| ``OriginalUrlReadInterface`` | ``getSeoOriginalUrl()`` | Returns a absolute url object to redirect to | |
| 24 | +| | | or create a canonical link from | |
| 25 | ++--------------------------------+---------------------------+----------------------------------------------+ |
| 26 | +| ``OriginalRouteReadInterface`` | ``getSeoOriginalRoute()`` | Return a ``Route`` object to redirect to | |
| 27 | +| | | or create a canonical link from | |
| 28 | ++--------------------------------+---------------------------+----------------------------------------------+ |
| 29 | +| ``ExtrasReadInterface`` | ``getSeoExtras()`` | Returns an associative array using | |
| 30 | +| | | ``property``, ``http-equiv`` and ``name`` | |
| 31 | +| | | as keys (see below from an example). | |
| 32 | ++--------------------------------+---------------------------+----------------------------------------------+ |
| 33 | + |
| 34 | +.. note:: |
| 35 | + |
| 36 | + The interfaces life in the ``Symfony\Cmf\Bundle\SeoBundle\Extractor`` |
| 37 | + namespace. |
| 38 | + |
| 39 | +An Example |
| 40 | +---------- |
| 41 | + |
| 42 | +Assume you have an ``Article`` object and you want to use both the ``$title`` |
| 43 | +and ``$date`` properties as page title and the ``$intro`` property as |
| 44 | +description, you can implement both interfaces and your result will be:: |
| 45 | + |
| 46 | + // src/Acme/BlogBundle/Document/Article.php |
| 47 | + namespace Acme\BlogBundle\Document; |
| 48 | + |
| 49 | + use Symfony\Cmf\Bundle\SeoBundle\Extractor\TitleReadInterface; |
| 50 | + use Symfony\Cmf\Bundle\SeoBundle\Extractor\DescriptionReadInterface; |
| 51 | + use Symfony\Cmf\Bundle\SeoBundle\Extractor\ExtrasReadInterface; |
| 52 | + |
| 53 | + class Article implements TitleReadInterface, DescriptionReadInterface, ExtraReadInterface |
| 54 | + { |
| 55 | + protected $title; |
| 56 | + protected $publishDate; |
| 57 | + protected $intro; |
| 58 | + |
| 59 | + // ... |
| 60 | + public function getSeoTitle() |
| 61 | + { |
| 62 | + return $this->title.' ~ '.date($this->publishDate, 'm-Y'); |
| 63 | + } |
| 64 | + |
| 65 | + public function getSeoDescription() |
| 66 | + { |
| 67 | + return $this->intro; |
| 68 | + } |
| 69 | + |
| 70 | + public function getSeoExtras() |
| 71 | + { |
| 72 | + return array( |
| 73 | + 'property' => array( |
| 74 | + 'og:title' => $this->title, |
| 75 | + 'og:description' => $this->description, |
| 76 | + ), |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +Creating Your Own Extractor |
| 82 | +--------------------------- |
| 83 | + |
| 84 | +To customize the extraction process, you can create your own extractor. Just |
| 85 | +create a class which implements the ``SeoExtractorInterface`` and tag it with |
| 86 | +``cmf_seo.extractor``: |
| 87 | + |
| 88 | +.. configuration-block:: |
| 89 | + |
| 90 | + .. code-block:: yaml |
| 91 | +
|
| 92 | + parameters: |
| 93 | + acme_demo.extractor.custom.class: Acme\DemoBundle\Extractor\MyCustomExtractor |
| 94 | +
|
| 95 | + services: |
| 96 | + acme_demo.extractor.custom: |
| 97 | + class: "%acme_demo.extractor.custom.class%" |
| 98 | + tags: |
| 99 | + - { name: cmf_seo.extractor } |
| 100 | +
|
| 101 | + .. code-block:: xml |
| 102 | +
|
| 103 | + <?xml version="1.0" ?> |
| 104 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 105 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 106 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 107 | + <parameters> |
| 108 | + <parameter key="acme_demo.extractor.custom.class">Acme\DemoBundle\Extractor\MyCustomExtractor</parameter> |
| 109 | + </parameters> |
| 110 | +
|
| 111 | + <services> |
| 112 | + <service id="acme_demo.extractor.custom" class="%acme_demo.extractor.custom.class%"> |
| 113 | + <tag name="cmf_seo.extractor"/> |
| 114 | + </service> |
| 115 | + </services> |
| 116 | + </container> |
| 117 | +
|
| 118 | + .. code-block:: php |
| 119 | +
|
| 120 | + $container->addParameter( |
| 121 | + 'acme_demo.extractor.custom.class', |
| 122 | + 'Acme\DemoBundle\Extractor\MyCustomExtractor' |
| 123 | + ); |
| 124 | +
|
| 125 | + $container->register('acme_demo.extractor.custom', '%acme_demo.extractor.custom.class%') |
| 126 | + ->addTag('cmf_seo.extractor') |
| 127 | + ; |
0 commit comments