ajshort/silverstripe-linkmapping — pull request #7 by jflearn (2014-02-10 18:11:42+00:00)
Title: Converted front controller to extension

This patch is a conversion of the linkmapping module from a custom front controller to a controller extension. I put this together because I ran into a problem with one of my sites where mapped URLs that resolved to a nested controller, but didn't actually exist, were creating 404s. You may want to consider it (or a variant thereof) for integration into the main line.

Here's what was happening for me:

Say I have a link mapping for "support/product1/contact.htm" -> "some/contact/page", and a CMS page exists at "support/product1". I would get a 404 error when accessing the URL, because the front controller is able to resolve "support/product1" to a valid controller, but there is no action method for that page for "contact.htm" (hasMethod() returns false). The front controller doesn't intercept this situation.

So, drawing inspiration from https://github.com/silverstripe-labs/silverstripe-redirectedurls, I migrated the linkmapping code to an extension which implements the "onBeforeHTTPError404". This catches the 404 a bit a bit further down during processing where it can be handled consistently regardless of how it originated.

As a side benefit, this implementation no longer requires the copy/pasted code from ModelAsController, so don't have to worry about keeping that function in sync with framework releases.

Aside: why didn't I just migrate to the redirectedurls module? They don't seem to provide the ability to select a CMS page as the redirect target, which is very convenient; it appears to just be path -> path. This seemed like the path of least resistance.

Also included a composer file if you want it--needed it for my site.