Dynamic routing without $Action param, fallback to page routing

Silverstripe Version: 4.5


I would like to be able to have a URL structure on a site that looks like mysite.com/$ID/$OtherID e.g. skipping the $Action param.

The challenge is that I don’t want that to break regular page routing. I still want to be able to have normal nested page URLs such as mysite.com/about/faqs/, which is handled by the modelascontrollercatchallroute Director routing rule.

Assuming I have a HomePage.php, I want requests that match the mysite.com/$ID/$OtherID pattern (and also just mysite.com/$ID) to be processed by HomePage.php, but if I can’t find an object that matches $ID then I want to fallback to the regular catch-all page routing.

Without this fallback, this pattern would cause most (or all?) regular page request to be routed through the home page controller and error out.

Is this possible with Silverstripe?

For now I’m looking at sub-classing ModelAsController to introduce the logic I want. Discovered an interesting effect below, I had to get hacky to introduce the change. Despite reading the Config API Docs, I really don’t understand why the first rule here doesn’t work. By the way, I tried all the variants of Before and After I could think of, didn’t seem to make a difference.

Name: custommodelascontrollercatchallroute
    # "$URLSegment//$Action/$ID/$OtherID": "CustomModelAsController" # This doesn't work, CustomModelAsController isn't used
    # "$URLPart//$Action/$ID/$OtherID": "CustomModelAsController" # This causes CustomModelAsController to be used, but then ModelAsController breaks because URLSegment is missing
    "$URLSegment//$Action/$ID/$OtherID/$Bogus": "CustomModelAsController" # This seems to work