silverstripe-googlesitemaps/docs/en/index.md

145 lines
4.3 KiB
Markdown
Raw Normal View History

# Google Sitemaps Module
2023-12-29 05:12:10 +01:00
Silverstripe CMS provides support for the Google Sitemaps XML system, enabling
Google and other search engines to see all pages on your site. This helps
2023-12-29 05:12:10 +01:00
your Silverstripe CMS website rank well in search engines, and to encourage the
information on your site to be discovered by Google quickly.
2023-12-29 05:12:10 +01:00
Therefore, all Silverstripe CMS websites contain a special controller which can
be visited: http://yoursite.com/sitemap.xml. This is not a file directly, but
rather a custom route which points to the GoogleSitemap controller.
See http://en.wikipedia.org/wiki/Sitemaps for info on the Google Sitemap
format.
2023-12-29 05:12:10 +01:00
Whenever you publish a new or republish an existing page, Silverstripe CMS can
automatically inform Google of the change, encouraging a Google to take notice.
2023-12-29 05:12:10 +01:00
If you install the Silverstripe CMS Google Analytics module, you can see if Google
has updated your page as a result.
2023-12-29 05:12:10 +01:00
By default, Silverstripe CMS informs Google that the importance of a page depends
on its position of in the sitemap. "Top level" pages are most important, and
the deeper a page is nested, the less important it is. (For each level,
Importance drops from 1.0, to 0.9, to 0.8, and so on, until 0.1 is reached).
In the CMS, in the Settings tab for each page, you can set the importance
manually, including requesting to have the page excluded from the sitemap.
## Configuration
2023-12-29 05:12:10 +01:00
Most module configuration is done via the Silverstripe CMS Config API. Create a
new config file `mysite/_config/googlesitemaps.yml` with the following outline:
2024-02-21 08:50:47 +01:00
```yml
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
use_show_in_search: true
```
You can now alter any of those properties to set your needs.
2024-02-21 08:50:47 +01:00
```yml
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
use_show_in_search: true
2024-02-21 08:50:47 +01:00
```
### Including DataObjects
The module provides support for including DataObject subclasses as pages in the
SiteTree such as comments, forum posts and other pages which are stored in your
database as DataObject subclasses.
To include a DataObject instance in the Sitemap it requires that your subclass
defines two functions:
2023-10-27 22:23:54 +02:00
- AbsoluteLink() function which returns the URL for this DataObject
- canView() function which returns a boolean value.
The following is a barebones example of a DataObject called 'MyDataObject'. It
assumes that you have a controller called 'MyController' which has a show method
to show the DataObject by its ID.
2023-10-27 22:23:54 +02:00
<?php
2017-10-19 22:05:18 +02:00
use SilverStripe\ORM\DataObject;
use SilverStripe\Control\Director;
2023-10-27 22:23:54 +02:00
class MyDataObject extends DataObject {
2023-10-27 22:23:54 +02:00
function canView($member = null) {
return true;
}
2023-10-27 22:23:54 +02:00
function AbsoluteLink() {
return Director::absoluteURL($this->Link());
}
2023-10-27 22:23:54 +02:00
function Link() {
return 'MyController/show/'. $this->ID;
}
}
After those methods have been defined on your DataObject you now need to tell
the Google Sitemaps module that it should be listed in the sitemap.xml file. To
2023-10-27 22:23:54 +02:00
do that, include the following in your \_config.php file.
2017-10-19 22:05:18 +02:00
use Wilr\GoogleSitemaps\GoogleSitemap;
2023-10-27 22:23:54 +02:00
GoogleSitemap::register_dataobject('MyDataObject');
If you need to change the frequency of the indexing, you can pass the change
frequency (daily, weekly, monthly) as a second parameter to register_dataobject(), So
instead of the previous code you would write:
2017-10-19 22:05:18 +02:00
use Wilr\GoogleSitemaps\GoogleSitemap;
2023-10-27 22:23:54 +02:00
GoogleSitemap::register_dataobject('MyDataObject', 'daily');
See the following blog post for more information:
http://www.silvercart.org/blog/dataobjects-and-googlesitemaps/
### Including custom routes
2023-12-29 05:12:10 +01:00
Occasionally you may have a need to include custom URLs in your sitemap for
your Controllers and other pages which don't exist in the database. To update
the sitemap to include those links call register_routes() with your array of
2023-12-29 05:12:10 +01:00
URLs to include.
2017-10-19 22:05:18 +02:00
use Wilr\GoogleSitemaps\GoogleSitemap;
2023-10-27 22:23:54 +02:00
GoogleSitemap::register_routes(array(
'/my-custom-controller/',
'/Security/',
'/Security/login/'
));
### Sitemapable
For automatic registration of a DataObject subclass, implement the `Sitemapable`
2023-12-29 05:12:10 +01:00
extension.
2023-10-27 22:23:54 +02:00
```
<?php
class MyDataObject extends DataObject implements Sitemapable
{
public function AbsoluteLink()
{
// ..
}
}
```