Replace $page->url() with model by the route patterns?

When using routes the $page->url() can be wrong in the templates/snippets/patterns. I have an idea that it might be possible to use a model to fix it by using the route patterns.

  • Would it be possible to extract the regex from the routes in a model?
  • Would it be a good idea to solve it like that? Or would it be too complex or slow?

Something like this?

foreach( $route->patterns as $pattern ) {
  if( preg_match($pattern, $page->url() ) ) {
    $page->url = preg_replace($pattern, $page->url());
  }
}

I wouldn’t recommend this. It’s not just slow, but also introduces a huge interconnected mess of logic that can be expressed a lot simpler by hardcoding. Especially for custom sites where those values won’t change.