|
| 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 an object implements a |
| 6 | +specific interface. The method required by that interface will return the |
| 7 | +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 | +| ``SeoDescriptionReadInterface`` | ``getSeoDescription()`` | Returns the meta description | |
| 17 | ++-----------------------------------+---------------------------+----------------------------------------------+ |
| 18 | +| ``SeoTitleReadInterface`` | ``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 | +| ``SeoOriginalUrlReadInterface`` | ``getSeoOriginalUrl()`` | Returns a absolute url object to redirect to | |
| 24 | +| | | or create a canonical link from | |
| 25 | ++-----------------------------------+---------------------------+----------------------------------------------+ |
| 26 | +| ``SeoOriginalRouteReadInterface`` | ``getSeoOriginalRoute()`` | Return a ``Route`` object to redirect to | |
| 27 | +| | | or create a canonical link from | |
| 28 | ++-----------------------------------+---------------------------+----------------------------------------------+ |
| 29 | + |
| 30 | +.. note:: |
| 31 | + |
| 32 | + The interfaces live in the ``Symfony\Cmf\Bundle\SeoBundle\Extractor`` |
| 33 | + namespace. |
| 34 | + |
| 35 | +An Example |
| 36 | +---------- |
| 37 | + |
| 38 | +Assume you have an ``Article`` object and you want to use both the ``$title`` |
| 39 | +and ``$date`` properties as page title and the ``$intro`` property as |
| 40 | +description, you can implement both interfaces and your result will be:: |
| 41 | + |
| 42 | + // src/Acme/BlogBundle/Document/Article.php |
| 43 | + namespace Acme\BlogBundle\Document; |
| 44 | + |
| 45 | + use Symfony\Cmf\Bundle\SeoBundle\Extractor\SeoTitleReadInterface; |
| 46 | + use Symfony\Cmf\Bundle\SeoBundle\Extractor\SeoDescriptionReadInterface; |
| 47 | + |
| 48 | + class Article implements SeoTitleReadInterface, SeoDescriptionReadInterface |
| 49 | + { |
| 50 | + protected $title; |
| 51 | + protected $publishDate; |
| 52 | + protected $intro; |
| 53 | + |
| 54 | + // ... |
| 55 | + public function getSeoTitle() |
| 56 | + { |
| 57 | + return $this->title.' ~ '.date($this->publishDate, 'm-Y'); |
| 58 | + } |
| 59 | + |
| 60 | + public function getSeoDescription() |
| 61 | + { |
| 62 | + return $this->title; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +Creating Your Own Extractor |
| 67 | +--------------------------- |
| 68 | + |
| 69 | +To customize the extraction process, you can create your own extractor. Just |
| 70 | +create a class which implements the ``SeoExtractorInterface`` and tag it with |
| 71 | +``cmf_seo.extractor``: |
| 72 | + |
| 73 | +.. configuration-block:: |
| 74 | + |
| 75 | + .. code-block:: yaml |
| 76 | +
|
| 77 | + parameters: |
| 78 | + acme_demo.extractor.custom.class: Acme\DemoBundle\Extractor\MyCustomExtractor |
| 79 | +
|
| 80 | + services: |
| 81 | + acme_demo.extractor.custom: |
| 82 | + class: "%acme_demo.extractor.custom.class%" |
| 83 | + tags: |
| 84 | + - { name: cmf_seo.extractor } |
| 85 | +
|
| 86 | + .. code-block:: xml |
| 87 | +
|
| 88 | + <?xml version="1.0" ?> |
| 89 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 90 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 91 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 92 | + <parameters> |
| 93 | + <parameter key="acme_demo.extractor.custom.class">Acme\DemoBundle\Extractor\MyCustomExtractor</parameter> |
| 94 | + </parameters> |
| 95 | +
|
| 96 | + <services> |
| 97 | + <service id="acme_demo.extractor.custom" class="%acme_demo.extractor.custom.class%"> |
| 98 | + <tag name="cmf_seo.extractor"/> |
| 99 | + </service> |
| 100 | + </services> |
| 101 | + </container> |
| 102 | +
|
| 103 | + .. code-block:: php |
| 104 | +
|
| 105 | + $container->addParameter( |
| 106 | + 'acme_demo.extractor.custom.class', |
| 107 | + 'Acme\DemoBundle\Extractor\MyCustomExtractor' |
| 108 | + ); |
| 109 | +
|
| 110 | + $container->register('acme_demo.extractor.custom', '%acme_demo.extractor.custom.class%') |
| 111 | + ->addTag('cmf_seo.extractor') |
| 112 | + ; |
0 commit comments