I’m John C Bland II

Husband, Father, Tech Author, Deacon.
Founder of Katapult Media, and full-stack polyglot developer.
Political Free Agents Podcast Host.

I create. I launch.

YouTube Channel

I post regular fun on YouTube like me playing the bass and anything else I find fun. 

Get Something Built

All project work goes through Katapult Media. Business is open. Let’s chat.

CFWheels always seems to make me smile with certain features. It isn’t 100% perfect (what is?) but it really makes CF dev fun. In this case, we’re taking a simple scenario of a user attempting to access a non-public url while not authorized to login. It could be from a bookmark or them simply trying to “hack” your system. Either way, once the user logs in they should automatically get to the page they want. Here’s how you can do it in Wheels.

First, you need to know Wheels passes all “route-specific” information in the params object. This object contains a lot of things but in this case we’re concerned with the route elements:

  • route
  • controller
  • action
  • [key, value, other custom property, etc]

We won’t get into routing but you can read more about it here. The docs page will clarify the last bullet point above.

Now, the normal flow to handle redirecting a user is to hold a reference to the page their requesting. You could do this with any of the normal ways in CF (storing CGI info, etc) but Wheels allows you to do the following:

...
<!--- save the current params to a session variable --->
<cfset session.redirectparams = params />
 
<!--- redirect the user to the login page --->
<cfset redirectTo(action="login") />
...

That goes in your code where you found the user not to be authorized. Of course your action may not be named “login” but you get the point. Next you need to redirect after the login is successful.

...
<cfif isDefined("session.redirectparams")>
     <!--- make a copy of the struct so the next line doesn't interfere --->
     <cfset var args = structcopy(session.redirectparams) />
 
     <!--- delete the struct from the session --->
     <cfset structDelete(session, "redirectparams") />
 
     <!--- redirect to the previously expected location --->
     <cfset redirectTo(argumentCollection=args) />
<cfelse>
     <!--- send user to default page --->
</cfif>
...

What this does is pass each property contained within your original params into redirectTo, which takes them and determines the best route to use then sends the user off.

This is a very simple feature to implement but your users will love you a little bit more for it. Enjoy!