Application.cfc
When a request is made to a ColdFusion page (.cfm/.cfc), the engine will look for a special file called Application.cfc and if found, it will include this file and execute it for you implicitly. This file is used to for setting application-wide settings, global settings, session/client storages, file mappings, datasources, and much more. These are created by placing them in the [This] scope and in the pseudo-constructor of the object. Application life-cycle event handlers which the engine will execute for you implicitly by you just creating the appropriate available methods. Usually this file is placed in the web root. On linux, the file name should be Application.cfc (with capital A). Any page in the same directory or sub-directories that is requested will execute this file implicitly.onApplicationStart: this method will be invoked only once and the very first time a user hits your application (after the CF is started/restarted). As this is called only once this is the right place to create application scope variables and there is no need for the "cflock" and "isDefined()" code in this section. You need to return true for this method, returning false will will not start the application. This method runs as a STA model (single threaded apartment module). notes: if this.name missing the application scope will be a pointer to the server object.
onSessionStart: this method is invoked the very first time a new user hits your application. A returning user with a valid current session will not trigger this method. this method can be used to set session scope data eg. shopping cart
onRequestStart: this method is invoked for each and every hit to the application. cf passes the script name as an argument to allow the programmer write some optional validation/security checks here. You need to return true, returning false will abruptly stop the current request. note: any variables set here are not be available in the requested page. (???). If you want a variable to be available in the requested page, set here set it in [request] scope.
onError: captures all errors/exceptions that are not captured by the cftry/cfcatch block(s). This methods has two arguments. The first is the exception object. The second is the name of the event that threw an error. You can write code to log/mail the error. Also it is a better idea to use cflocation tab to redirect the user a user friendly error page. Please note onError takes precendence over any error handler code other than cftry/cfcatch.
onApplicationEnd: this method is invoked by the cf engine whenever the application timesout or when you stop/restart your application. The method receives the application scope as an argument. Any changes to the passed argument applicationScope will be reflected in the actual application scope as the application scope's pointer is passed in the argument. note: at this point the application scope is not available.
onSessionEnd: this method is invoked whenever a session timesout. application scope (as a pointer) and session scope is passed to this method. Please do not modify the application scope directly here. note: at this point both application and session scope are not available.
onRequestEnd: this method will be auto-invoked at the end of every request. this is akin to having a OnRequestEnd.cfm file in the folder.
onRequest: (suggesstion) Do not use this method. this method is basically used to override the requested page so that an alternate page can be loaded in its place. If you want the original requested page to be executed then you should use cfinclude to include the page using the passed page argument. Note: this method will break Flash Remoting and Web Service calls.
workarounds:
1. to get around this create another subdirectory and add a new Application.cfc without the onRequest method.
This: The [This] structure contains all data about the CFC and what its methods.
Application.cfc
<cfcomponent output="false">
<cfscript>
this.name = "myApplicationName";
this.applicationTimeout = createTimeSpan(8,0,0,0);
this.clientManagement = false;
//this.clientStorage = "registry";
//this.loginStorage = "session";
this.sessionManagement = false;
//this.sessionTimeout = createTimeSpan(0,0,0,0);
this.setClientCookies = false;
this.setDomainCookies = false;
this.scriptProtect = false;
this.secureJSON = false;
this.secureJSONPrefix = "";
this.welcomeFileList = "";
</cfscript>
<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfreturn true />
</cffunction>
<cffunction name="onApplicationEnd" returnType="void" output="false">
<cfargument name="applicationScope" required="true" />
<cfreturn />
</cffunction>
<cffunction name="OnSessionStart" access="public" returntype="void" output="false">
<cfreturn />
</cffunction>
<cffunction name="OnSessionEnd" access="public" returntype="void" output="false">
<cfargument name="SessionScope" type="struct" required="true" />
<cfargument name="ApplicationScope" type="struct" required="false" default="#StructNew()#" />
<cfreturn />
</cffunction>
<cffunction name="onRequestStart" returnType="boolean" output="true">
<cfargument name="thePage" type="String" required="true" />
<cfreturn true />
</cffunction>
<!---cffunction name="onRequest" returnType="void" output="false">
<cfargument name="thePage" type="String" required="true" />
<cfreturn />
</cffunction--->
<cffunction name="onRequestEnd" returnType="void" output="false">
<cfargument name="thePage" type="String" required="true" />
<cfreturn />
</cffunction>
<cffunction name="onMissingTemplate" returnType="boolean" output="false">
<cfargument name="targetpage" type="String" required="true" />
<cfreturn true />
</cffunction>
<!---cffunction name="onError" returnType="void" output="false">
<cfargument name="exception" required="true" />
<cfargument name="eventname" type="String" required="true" />
<cfreturn />
</cffunction--->
</cfcomponent>
Ginger CMS
the future of cms, a simple and intuitive content management system ... ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ... CFTurbine
cf prototyping engine, generates boilerplate code and views ... Search Engine LITE
create your own custom search engine for your web site ... JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ... Validation Library
complete validation library for your web forms ...
the future of cms, a simple and intuitive content management system ... ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ... CFTurbine
cf prototyping engine, generates boilerplate code and views ... Search Engine LITE
create your own custom search engine for your web site ... JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ... Validation Library
complete validation library for your web forms ...