. 4 minutes May 29, 2023

How to Set Up Dynamic SEO URLs in Shopware

A dynamic SEO URL is a URL of a web page that is dynamically generated from the database. It has to change every now and then. Here, we will discuss how we define our own SEO URLs.

For this scenario, you can make use of the Shopware built-in SeoUrlRoute classes, which hold all the necessary information about your dynamic route.

Let’s have a look at an example of the stories section. Each story page has a different seo url with respect to language. So the seo URL has to be changed based on language.

First need to create a translatable entity for stories with seo_url field:

Following will be the controller path to access the stories detail page in front end.

Here as you can see, the route has got an id parameter. We will be receiving stories id instead of seo_url and corresponding stories will be fetched using id.

Then will create a migration to insert an entry inside the seo_url_template table.

Then will create SeoUrlRoute classes, which hold all necessary information about your dynamic route and will then create the respective seo_url entries automatically.

Will look at each step by step in detail. The custom “SeoUrlRoute” class has to implement the SeoUrlRouteInterface, which contains the following methods 

  • getConfig()

Here you have to return an instance of SeoUrlRouteConfig. Then contains,

  • Your entity’s definition. 
  • Technical name of the route to be used.
  • Desired SEO path.
  • prepareCriteria()

Here you can adjust the criteria instance, which will be used to fetch your entities.For example you could add a filter on an active field and therefore only generate SEO URLs for active entities. Also you can add associations here.

  • getMapping()

In this method, you have to return an instance of SeoUrlMapping.

It has to contain the actually available data for the SEO URL template. If you’re using    

a variable entry.seoURl in the SEO URL template, you have to provide the data for the key entry here.

Following will be the services.xml entry for StorySeoUrlRoute. It has to be registered to the container using the tag shopware.seo_url.route.


Story subscriber

Every time when your entity is written, we have to let Shopware know that we want to generate the SEO URLs for those entities. Same if your entity is deleted, we need to update the same as well. This is done by reacting to the DAL events of your custom entity, to be specific we’re going to use the written event and deleted event.

For written events and deleted events of our custom entity, we need to provide the entity’s technical name with .written, .deleted suffix. Every time it is executed, we’re just using the said update method of the SeoUrlUpdater.  

The subscriber has to be registered to the container for using it (services.xml)

The following example shows the dynamic seo url for a single story in English and German language in both the backend and front.

Table entries for both seo_url_template  and seo_url

 

seo_url_template

seo_url

So when you execute the seo URL path (eg: story/story-in-english) in the browser, shopware by default will be checking in seo_url table and corresponding field path_info  ( eg: /story/78f619ff640e4cf7a5590cb90a290e75) will be processed to the controller. And in the controller, we could get it as an entity id.

So, by following these methods you could easily manage Dynamic SEO URLs in your custom entities in shopware.

shopware
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
CEO
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Arsha Content writer

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.