[Coldbox 7] Router: Can you Redirect to a CFM Page?

I am working on adding Coldbox to a legacy app, and I’d like to set up a Coldbox route that redirects to an old .cfm file. However, I noticed that the toRedirect() method expects an event and converts all periods into slashes like this:

route( "foo/bar" ).toRedirect( "foo_bar.cfm" );
Redirects To: http://127.0.0.1:62923/foo_bar/cfm

I experimented with turning off valuePairTranslation and using a closure for toRedirect() like this, but it did not work:

route( "foo/bar" ).toRedirect( function( route, params, event ) {  
    return "foo_bar.cfm";
} );

Now I could handle this at the web server level, by creating some manual redirects. However, I am using Commandbox in development and IIS in production, so I would need to create twice the number of redirect rules. Additionally, I think it would be cool if this could be done in Coldbox as well for more consolidated legacy app migration tracking purposes.

Perhaps the router could have a new method toUri() or there could be a method similar to turning off valuePairTranslation, but instead it wouldn’t treat routes as events like eventTranslation( false ).

Is there another way to handle this in Coldbox, or am I stuck making changes at the web server level for now?

You can just route directly to a view:

route( "foo/bar" ).toView( "path/to/foo_bar" );
1 Like

Good call @Andrew_Kretzer. I think that option restricts me to the /views/ folder though. Some of the legacy files in this app are sitting in the web root.

However, your post gave me an idea:
I could create a “shell” view which would simply cfinclude the legacy page, so this could be a viable option! Thank you!

It is definitely not restricted to the views folder. You can point it to anywhere in your app.

Wow you’re right! I mistakenly assumed toView() forced the /views/ convention.

Example:

route( "foo/bar" ).toView(
    view="/foo_bar", // assumes /foo_bar.cfm in the root
    nolayout = true // no need to render the layout
);

I created a PR to get an example of how to do this in the docs.

Thanks Andrew!

I just discovered another way to do this is to use relocate() inside of the toRedirect() routing method, if you want to perform a redirect instead of a rewrite, like this:

// the URL will change to /foo_bar.cfm
route( "foo/bar" ).toRedirect( function( route, params, event ) {
    relocate( uri="/foo_bar.cfm" );
} );

Yep, you can install Colbox and run your whole app through the Router without ever using a handler etc. if you really wanted to do so…