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:
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 thefileRead(); 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.
Reader Comments
All mapped paths feel magical to me. I avoid them.
@Chris, yeah, I tend to try and have them mirror an actual directory. So for example, I'll map
libto alibfolder that's just harder to get to with a dot-notation (required by thenewoperator):So that I can then use
new lib.foo.Bar()from anywhere in the app. And at least I know thatlibisn'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 →