2Hats . 4 minutes

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.

blog
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.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Aneesh ceo

    Stay In The Loop!

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