Create globally unique URLs within CFWheels applications

One great feature of the CFWheels framework is being able to override inbuilt functions, yet still be able to call the core function from within. This allows you to slightly tweak core functions without having to copy, paste & butcher core code, which would 1) go against the DRY principle, and 2) create possible legacy issues when the core functions are updated.

The example below tweaks the urlFor() function by adding a random token to all urls generated within a CFWheels application. This can be handy to avoid page caching as every url is unique.

Because the urlFor() function is used within linkTo(), redirectTo() and startFormTag(), this single tweak trickles down to all CFWheels functions that generate urls.

// controllers/Controller.cfc

/**
 * Hint: Appends a token to all urlFor() calls
 */

public any function urlFor() {

	// create your random string
	var randomToken = LCase(Replace(CreateUUID(), "-", "", "all"));

	// defaults
	param name="arguments.params" default="";
	param name="arguments.token" default="true";

	// add the token to the URL unless told otherwise.
	if (arguments.token) {
		arguments.params = ListAppend(arguments.params, randomToken, "&");
	}

	// pass any args to the core urlFor helper
	return core.urlFor(argumentCollection=arguments);
}

Your urls that once looked like this /mypage, now magically look like /mypage?d9e4bd5a3b78427dab33fe02e0c14b5a=

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s