How can I alter the routing in Craft? I need to add a (variable) segment to every possible URL.
The reason is we have a multi lingual site with multiple regions. We need to know the correct region in every template to know which data to load. There are many "multi site/multi region hacks" to find on SE to achieve this, but we don't really like any of them.
We want to add the region segment to the URL after the language segment: eg. https://myurl.com/en/region
Problem is of course re-routing that URL.
- One option would be to add dynamic routing, but than I need to do this for every template in every language (for translation purposes) and that seems a lot of work. Also, when a new language or template is added later on, all routes must be set anew.
- I came upon the
getElementRoutehook. That allows me to change the URL as I would change it in my Twig template but nothing more it seems.
- A query string is also an option but that's just not pretty.
I would need something like a global dynamic route without a specified template to tell Craft: whatever route you get, just disregard segment 2 (region) and do the routing as you would otherwise do.
So, is there a way to add a segment to Craft without messing up all the routing? Maybe some very base level function?
I was thinking about using the
getElementRoute() hook to alter the URL by adding the region segment.
And than somehow create a route dynamicaly in the
registerSiteRoutes() hook that corresponds to the URL I retrieved in
I can do the first part, but not the second.