Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 0b45fa3

Browse files
committed
Finished rewriting SeoBundle
1 parent c2a536b commit 0b45fa3

File tree

1 file changed

+120
-122
lines changed

1 file changed

+120
-122
lines changed

bundles/seo/introduction.rst

Lines changed: 120 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ The only thing to do now is to use the twig helper in your templates:
7272
<html>
7373
<head>
7474
{{ sonata_seo_title() }}
75+
7576
{{ sonata_seo_metadatas() }}
76-
{{ sonata_seo_link_canonical() }} // needed later
7777
</head>
7878
<body>
7979
<p>Some page body.</p>
@@ -125,9 +125,9 @@ Both ways are documented in detail in seperate sections:
125125
Choosing the Original Route Pattern
126126
-----------------------------------
127127

128-
Search engines punish you, when you provide the same content under several
129-
URLs. The CMF allows you to have several URLs for the same content if you
130-
need that. There are two solutions to avoid penalties with search engines:
128+
Search engines don't like it when you provide the same content under several
129+
URLs. The CMF allows you to have several URLs for the same content if you need
130+
that. There are two solutions to avoid penalties with search engines:
131131

132132
* Create a canonical link that identifies the original URL:
133133
``<link rel="canonical" href="/route/org/content">``;
@@ -162,23 +162,13 @@ you want to change that to redirect instead, you can set the
162162
),
163163
);
164164
165-
.. todo
166-
167-
Configuring the Default Values
168-
------------------------------
165+
Defining a Default
166+
------------------
169167

170-
You know now how to work with objects configuring SEO data. However, in some
171-
cases the object doesn't provide any information. Then you have to configure a
172-
default. You can configure the defaults for the SonataSeoBundle, these
173-
defaults will be overriden by the metadata from the CmfSeoBundle. However, you
174-
might want to combine.
175-
176-
Visiting the site with the url ``/seo-content`` (same template shown above)
177-
will show a Page with "Documents own tile" as title, "This ist the text for
178-
the description meta tag" in the description, "Seo, Content" in the keywords
179-
and a canonical link with ``href="/original/url/of/content"``. But what about
180-
some default string to just concatenate defaults and documents own values?
181-
Just add some more configs to the cmf_seo configuration section.
168+
You've learned everything about extracting SEO information from objects.
169+
However, in some cases the object doesn't provide any information or there is
170+
no object (e.g. on a login page). For these cases, you have to configure a
171+
default value. These default values can be configured for the SonataSeoBundle:
182172

183173
.. configuration-block::
184174

@@ -187,170 +177,178 @@ Just add some more configs to the cmf_seo configuration section.
187177
# app/config/config.yml
188178
sonata_seo:
189179
page:
180+
title: A Default Title
190181
metas:
191182
names:
192183
keywords: default, sonata, seo
193-
cmf_seo:
194-
title: default_title_key
195-
description: default_title_key
196-
197-
.. code-block:: xml
198-
199-
<!-- app/config/config.xml -->
200-
<container xmlns="http://symfony.com/schema/dic/services">
201-
<config
202-
xmlns="http://cmf.symfony.com/schema/dic/seo"
203-
title="default_title_key"
204-
description="default_title_key">
205-
</config>
206-
</container>
184+
description: A default description
207185
208186
.. code-block:: php
209187
210188
// app/config/config.php
211189
$container->loadFromExtension(
212190
'sonata_seo', array(
213191
'page' => array(
192+
'title' => 'A Default Title',
214193
'metas' => array(
215194
'names' => array(
216-
'keywords' => 'default, key, other',
195+
'keywords' => 'default, key, other',
196+
'description' => 'A default description',
217197
),
218198
),
219199
),
220200
),
221-
'cmf_seo' => array(
222-
'title' => 'default_title_key',
223-
'description' => 'default_description_key',
224-
),
225201
);
226202
227-
As you will notice, you got the opportunity to set Symfony translation key for
228-
your default values for title and description. So you will got
229-
Multi-Language-Support out of the box. Just define your values for default
230-
title/description as translations:
231-
232-
.. code-block:: xml
233-
234-
<!-- app/Resources/translations/messages.en.xliff -->
235-
<?xml version="1.0" encoding="utf-8"?>
236-
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
237-
<file source-language="en" target-language="en" datatype="plaintext" original="messages.en.xliff">
238-
<body>
239-
<trans-unit id="default_title_key">
240-
<source>default_title_key</source>
241-
<target>%content_title% | Default title</target>
242-
</trans-unit>
243-
<trans-unit id="default_description_key">
244-
<source>default_description_key</source>
245-
<target>Default description. %content_description%</target>
246-
</trans-unit>
247-
</body>
248-
</file>
249-
</xliff>
250-
251-
If you want to concatenate your documents values with the default ones you
252-
need them as parameters in you translation target.
253-
254-
.. tip::
255-
256-
If you does not what to open a translation file for two entry, just set
257-
``Default title | %%content_title%%``or ``Default description.
258-
%%content_description%%``.
259-
260-
For changing the default translation domain (messages), the SeoBundle provides
261-
a configuration value:
203+
The Standard Title and Description
204+
----------------------------------
205+
206+
Most of the times, the title of a site has a static and a dynamic part. For
207+
instance, "The title of the Page - Symfony". Here "- Symfony" is static and
208+
"The title of the Page" will be replaced by the current title. It is of course
209+
not nice if you need to add this static part to all your titles in documents.
210+
211+
That's why the CmfSeoBundle provides standard titles and descriptions. When
212+
using these settings, there are 2 placeholders available: ``%content_title%``
213+
and ``%content_description%``. This will be replaced with the title extracted
214+
from the content object and the description extracted from the content object.
215+
216+
For instance, to configure the titles of the symfony.com pages, you would do:
262217

263218
.. configuration-block::
264219

265220
.. code-block:: yaml
266221
267222
# app/config/config.yml
268223
cmf_seo:
269-
translation_domain: AcmeDemoBundle
224+
title: "%%content_title%% - Symfony"
270225
271226
.. code-block:: xml
272227
273228
<!-- app/config/config.xml -->
274-
<container xmlns="http://symfony.com/schema/dic/services">
275-
<config
276-
xmlns="http://cmf.symfony.com/schema/dic/seo"
277-
translation-domain="AcmeDemoBundle">
278-
</config>
279-
</container>
229+
<config xmlns="http://cmf.symfony.com/schema/dic/seo"
230+
title="%%content_title%% - Symfony"
231+
/>
280232
281233
.. code-block:: php
282234
283235
// app/config/config.php
284-
$container->loadFromExtension(
285-
'cmf_seo' => array(
286-
'translation_domain' => 'AcmeDemoBundle',
287-
),
288-
);
236+
$container->loadFromExtension('cmf_seo', array(
237+
'title' => '%%content_title%% - Symfony',
238+
));
289239
290-
Preface
291-
-------
240+
.. caution::
292241

293-
Search engines punish you when you provide the same content under several
294-
URLs. The CMF allows you to have several URLs for the same content if you
295-
need that. There are two solutions to avoid penalties with search engines:
242+
Be sure to escape the percentage characters by using a double percentage
243+
character, otherwise the container will try to replace it with the value
244+
of a container parameter.
296245

297-
* Create a canonical link that identifies the original URL:
298-
``<link rel="canonical" href="/route/org/content">``;
299-
* Redirect to THE original url.
246+
This syntax might look familiair if you have used with the Translation
247+
component before. And that's correct, under the hood the Translation component
248+
is used to replace the placeholders with the correct values. This also means
249+
you get Multi Language Support for free!
250+
251+
For instance, you can do:
252+
253+
.. configuration-block::
254+
255+
.. code-block:: yaml
300256
301-
Both take care on search engines, which does not like it to have same content
302-
under different routes.
257+
# app/config/config.yml
258+
cmf_seo:
259+
title: seo.title
260+
description: seo.description
261+
262+
.. code-block:: xml
263+
264+
<!-- app/config/config.xml -->
265+
<config xmlns="http://cmf.symfony.com/schema/dic/seo"
266+
title="seo.title"
267+
description="seo.description"
268+
/>
269+
270+
.. code-block:: php
271+
272+
// app/config/config.php
273+
$container->loadFromExtension('cmf_seo', array(
274+
'title' => 'seo.title',
275+
'description' => 'seo.description',
276+
));
277+
278+
And then configure the translation messages:
279+
280+
.. configuration-block::
303281

304-
The SeoBundle uses sonatas SeoBundle and its TwigHelper to render the the
305-
`SeoMetadata` into your Pag. So you should have a look at the documentation at
306-
`sonata seo documentation_`
282+
.. code-block:: xml
283+
284+
<!-- app/Resources/translations/messages.en.xliff -->
285+
<?xml version="1.0" encoding="utf-8"?>
286+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
287+
<file source-language="en" target-language="en" datatype="plaintext" original="file.ext">
288+
<body>
289+
<trans-unit id="seo.title">
290+
<source>seo.title</source>
291+
<target>%content_title% | Default title</target>
292+
</trans-unit>
293+
<trans-unit id="seo.description">
294+
<source>seo.description</source>
295+
<target>Default description. %content_description%</target>
296+
</trans-unit>
297+
</body>
298+
</file>
299+
</xliff>
300+
301+
.. code-block:: php
302+
303+
// app/Resources/translations/messages.en.php
304+
return array(
305+
'seo' => array(
306+
'title' => '%content_title% | Default title',
307+
'description' => 'Default description. %content_description',
308+
),
309+
);
310+
311+
.. code-block:: yaml
312+
313+
# app/Resources/translations/messages.en.yml
314+
seo:
315+
title: "%content_title% | Default title"
316+
description: "Default description. %content_description%"
307317
308-
For redirects instead of canonical links (default) set the following option:
318+
For changing the default translation domain (messages), you should use the
319+
``cmf_seo.translation_domain`` setting:
309320

310321
.. configuration-block::
311322

312323
.. code-block:: yaml
313324
314325
# app/config/config.yml
315326
cmf_seo:
316-
original_route_pattern: redirect
327+
translation_domain: AcmeDemoBundle
317328
318329
.. code-block:: xml
319330
320331
<!-- app/config/config.xml -->
321332
<container xmlns="http://symfony.com/schema/dic/services">
322-
<config
323-
xmlns="http://cmf.symfony.com/schema/dic/seo"
324-
original-route-pattern="redirect">
325-
</config>
333+
<config xmlns="http://cmf.symfony.com/schema/dic/seo"
334+
translation-domain="AcmeDemoBundle"
335+
/>
326336
</container>
327337
328338
.. code-block:: php
329339
330340
// app/config/config.php
331341
$container->loadFromExtension(
332342
'cmf_seo' => array(
333-
'original_route_pattern' => 'redirect',
343+
'translation_domain' => 'AcmeDemoBundle',
334344
),
335345
);
336346
337-
This value will cause a redirect to the url persisted in the ``originalUrl``
338-
property of the ``SeoMetadata``.
339-
340-
The SeoMetadata contains a form type for your Symfony Form. Just create you
341-
form with the following key:
342-
343-
.. code-block:: php
344-
345-
$formBuilder
346-
...
347-
->add('seoMetadata', 'seo_metadata', array('label' => false));
348-
...
349-
;
350-
351-
For SonataAdminBundle user the SeoBundle provides an admin extension to add
352-
that form to your form configuration.
347+
Conclusion
348+
----------
353349

350+
That's it! You have now created a SEO optimized website using nothing more
351+
than a couple of simple settings.
354352

355353
.. _`SonataSeoBundle`: https://github.com/sonata-project/SonataSeoBundle
356354
.. _`with composer`: http://getcomposer.org

0 commit comments

Comments
 (0)