Skip to main content
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Jared Rypka-Hauer
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Jared Rypka-Hauer

Using Per-Application Mappings To Alias Files In ColdFusion

By
Published in Comments (2)

For as long as I can remember, I've been using the per-application mappings in ColdFusion to alias directories. And then, using those aliases to prefix file paths and CFInclude template paths. Yesterday, however, as I was looking at consuming configuration files in ColdFusion, it occurred to me that per-application mappings might be able to do more than provide prefixes—they might be able to alias full file paths. I can't believe this never occurred to me before; so, I wanted to try it out in both Adobe ColdFusion and Lucee CFML.

To test this, all I'm going to do is create a per-application mapping, /app.conf, that points to the text file, test.txt:

component
	output = false
	hint = "I define the application settings and event handlers."
	{

	// Define the application settings.
	this.name = "AppMappingsDemo";
	this.applicationTimeout = createTimeSpan( 0, 1, 0, 0 );
	this.sessionManagement = false;
	this.setClientCookies = false;

	this.directory = getDirectoryFromPath( getCurrentTemplatePath() );

	// In this exploration, I'm creating a per-application mapping directly to a FILE -
	// not to a directory (which is what I usually do). Note that the path and filename
	// are not a reflection of where the file actually resides on the server.
	this.mappings = {
		"/app.conf": ( this.directory & "../config/test.txt" )
	};

}

As you can see, /app.conf maps to test.txt, which actually lives above the current directory. Note that my mapped path includes the ../ relative traversal segment.

Now, in a separate CFML file in the same application, I'm going to try and read the /app.conf file:

<cfscript>

	// Try to read in the content of the mapped file! Hold onto your butts!
	writeDump(
		label = "/app.conf data",
		var = fileRead( expandPath( "/app.conf" ), "utf-8" )
	);

</cfscript>

Now, when we run this code in Lucee CFML, we get the following output:

CFDump of file contents identified by mapped file.

Oh my chickens! It worked! I was able to read the contents of the file by exclusively using the per-application mapping. This also works in Adobe ColdFusion; but, I'm going to omit the output of that experience since it doesn't add value to the conversation.

Aside: I will mention, however, that Adobe ColdFusion only worked if I included the expandPath() portion of the code in the fileRead(); and, Lucee CFML works without it. That said, Lucee CFML is historically more amenable to relative file paths.

In retrospect, I didn't have any reason to believe that this wouldn't work; only, that it never occurred to me and I never tried it before. With that said, I would caution that this code is a bit "magical"; and, it might confuse future developers who are looking for /app.conf and can't seem to find it in the file system. So, if you go this route, proceed with caution.

Want to use code from this post? Check out the license.

Reader Comments

15,848 Comments

@Chris, yeah, I tend to try and have them mirror an actual directory. So for example, I'll map lib to a lib folder that's just harder to get to with a dot-notation (required by the new operator):

this.mappings = {
	"/lib": "../../lib"
};

So that I can then use new lib.foo.Bar() from anywhere in the app. And at least I know that lib isn't entirely made up - it's just "starting" the path in a parent directory.

Which is why the idea of aliasing a file name definitely seems sketchy 😆 in a way, it's asking for trouble.

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel