Custom Sitemap Extension

We use a commandline script for generating the Sitemap contents of a project. We provide you with some standard Sitemap features for generation in that script, for example, generating Sitemap for all Nodes, Products, Categories, etc. In some cases, it doesn't provide you enough flexibility so you can write your own extension.

To do this, you'll need to define your extension (like all our other extensions) following the interface here:  Frontastic\Catwalk\FrontendBundle\Domain\SitemapExtension (/Catwalk/config/src/php/FrontendBundle/Domain/SitemapExtension.php).

The name is important to determine what you'll generate by that extension and will be used inside the filename of the sitemap xml-file.

getEntries() will return the needed URLs that should be added to your sitemap. It may also contain an array of image-URLs that should be added to the sitemap URL-reference as well.

The generation of the sitemap then takes place in our Sitemap generation command. You'll need to register it in order to let the command know that there is an extension that should be executed.

Registering Your Extension

To register your extension so that it can be executed, you need to add a tag to the corresponding service inside the  services.xml:

<!-- [...] -->
	<service id="..." class="...">
		<argument type="...">
		<!-- [...] -->
		<tag name="frontend.sitemapExtension"/>
	</service>
<!-- [...] -->
	

⚠️You also need to run the script with the  --with-extensions parameter. Otherwise extensions won't be executed!

Adding cronjob for Sitemap Generation

In order to get this executed regularly you'll need to add an entry to the crontab for this. The crontab could be added or found inside the project's config-directory: config/crontab.

There you need to add an entry for the Sitemap generation command and adjust it to your needs. For example, it may look like this:

29 */2 * * * php bin/console frontastic:sitemap:generate --exclude "^/development" --with-nodes --with-products --with-categories --with-extensions public/sitemaps


‹ Back to Article List

Next Article ›

Extending the webpack Configuration

Still need help? Contact Us Contact Us