Skip to main content
Known Participant
April 23, 2014
Question

Moving to CF10, Need help with Application.cfc

  • April 23, 2014
  • 1 reply
  • 1203 views

I've been googling about how to work with Application.cfc since last week but I still have some questions and I can't find the answers.

My application is under the root (in unix) and there are many subfolders underneath it. Each sub-folder is hosting a different web application.

From what I read, I can create 1 root Application.cfc and then on subsequent sub-folder, when I need to have another Application.cfc on that level, I can create ProxyApplication (see below) and then create a sub-folder level Applicatin.cfc

So, when I set an application.DSN on my root Application.cfc, using proxyApplication I don't have to reset this dsn again in my sub folder level Application.cfc

Since my loginform.cfm and loginaction.cfm is right under root directory too,  I also set OnsessionStart in the root Application.cfc to handle user login. Then this means, I don't have to reset session variable again anywhere because session.username, etc has been set on the highest level.

Is this correct?

In addition, Am I correct when I do the following:

1. Since I have root level and sub-folder level Application.cfc, I should set this.name with a different name, am I right?

    On the root Application.cfc I set this.name = "StudentServices" because this represent the global application

    On the sub-folder level's Application.cfc, I set this.name to "StudentServices_stdLoad" becaus this sub-folder only handle student load application.

2. On the root Application.cfc, I set the DSN to the application scope. So on the sub-folder level Application.cfc I can check if a particular db is working or not

    because as awhole, in the global sense, this web application uses more than one Databases. Each sub-folder may use a database that is dfferent than the other sub folder.

Am I doing the right thing? Please advice

Below is example of what I have, Thank you!

I created a root Application.cfc under the root directory: 

<CFCOMPONENT displayname="Application" output="true" hint="My Root Application component">

   <!--- Set up the application --->

   <cfset THIS.Name = "StudentServices" />

   <cfset THIS.ApplicationTimeout = CreateTimeSpan(0,0,30,0) />

   <cfset THIS.SessionManagement = true />

   <cfset THIS.SetClientCookies = false />

  

   <cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false">

 

   <cfset application.MainDSN = "DSN1">

   <cfset application.ReportDSN = "DSN2">

   <cfreturn true/>

</cffunction>



<cffunction name="onApplicationEnd" output="false">

    <cfargument name="applicationScope" required="true">

</cffunction>



<cffunction name="onSessionEnd">

</CFCOMPONENT>





Then, in this root directory I also created a ProxyApplication:



<!--- it's empty and it Serves merely to create an alias for your root /Application.cfc --->

<cfcomponent name="ApplicationProxy" extends="AdvancementServices.Application">

</cfcomponent>



Then in the Sub-Directory, I can create a sub-folder level Application.cfc extending the root Application.cfc:



<CFCOMPONENT displayname="Application" extends="ApplicationProxy">

    <!--- Set up the sub-folder application --->

    <cfset THIS.Name = "StudentServices_stdLoad"/>

    <cfset THIS.ApplicationTimeout = CreateTimeSpan(0,0,30,0) />

    <cfset THIS.SessionManagement = true/>

    <cfset THIS.SetClientCookies = false/>

    <cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false">

       <!--- ****** Testing whether the ADVUPGRD is accessible by selecting some data.****** --->

       <cftry>

       <cfquery name="TestMain_DSN" datasource="#application.MainDSN#" maxrows="2">

         SELECT Count(*)

        

FROM MyTable

       </

cfquery>

         <!--- If we get a database error, report an error to the user, log the error information, and do not start the application. --->

        <cfcatch type="database">

          <cflog file="#this.name#" type="error" text="Main DSN is not available. message: #cfcatch.message# Detail: #cfcatch.detail# Native Error: #cfcatch.NativeErrorCode#" >

         <cfthrow message="This application encountered an error when connecting to the Main Database. Please contact support." />

         <cfreturn false>

       </cfcatch>

     </cftry>

     <cflog file="#this.name#" type="Information" text="Application #this.name# Started">

     <cfreturn true/>



   </cffunction>



</CFCOMPONENT>









   <cfargument name = "SessionScope" required=true/>

   <cfargument name = "AppScope" required=true/>

</cffunction>

<cffunction name="OnSessionStart" access="public" returntype="void" output="false">

  <CFSET session.UserGroup = ""/>

  <CFSET session.UserName = ""/>

  <CFSET session.currentPage = ""/>

  <CFSET session.loggedin = "No"/>

  <CFSET session.userrights = ""/>

  <cfreturn/>

</cffunction>

This topic has been closed for replies.

1 reply

Carl Von Stetten
Brainiac
April 24, 2014

If each "subapplication" has a different name (e.g.: <cfset THIS.Name = "StudentServices_stdLoad"/> ), then they will each have their own sessions separate from any sessions created by the root-level Application.cfc.  So any session variables you set in OnSessionStart() in the root Application.cfc will not be seen from any pages within the "subapplication".  In order to share sessions, all "subapplications" must use the same name as the root-level Application.cfc.

Just as an aside, in the future please strip down the whitespace when you post code to review to reduce the amount of scrolling required.  It will make it easier for others to examine and offer help.

-Carl V.

aleckenAuthor
Known Participant
April 25, 2014

Hi Carl, Thak you for your valuable advice.

Regarding the whitespace I did not create that. Once I submitted my entry those whitespaces were created. I may have to create my entry in Notepad then pasted there to avoid these whitespaces, especially for the codes (?).

Your explanation about This.Name. If every sub-application will not be able to see the session set in the root Application.cfc then what is the purpose of extending root Application in the sub-folder level? I'm even more confuse than ever now.

Carl Von Stetten
Brainiac
April 25, 2014

@alecken,

Can't speak to the how the whitespace gets generated.  I guess once you submit a message, if you see a bunch of whitespace, you can go back and edit the message to remove it.  It's not a big deal if there is whitespace, it just makes it harder to read the code.

With regard to the root Application.cfc vs. sub-application Application.cfc and THIS.name, it can be a bit confusing.  It might be easier to think about it going from the top level down to the lowest level. 

An "application" has a unique name that defines it as separate from all other applications running on the same ColdFusion instance.  If your applications use sessions (and not all applications have to), each application will have it's own collection of sessions effectively walled-off from any other application's sessions.  If you change the THIS.name in Application.cfc (from here on out I'll refer to it as App.cfc for brevity) and hit a page in your application, it starts a new application on the ColdFusion instance, and will have a new session collection.  So, in your case, if your sub-application App.cfc defines a different THIS.name from the root App.cfc, you start a completely new application on that ColdFusion instance, and a new session collection.

It looks to me like what you really want is all sub-applications to share some common settings, but still operate independently from each other.  Can you confirm whether this is your goal? If it is, I can think of a different way to make this work that will allow each sub-application to operate as an independent application.

One other thing, and this may just be a copy/paste error - in your sub-application App.cfc, the <cfargument> tags following your onSessionStart() function should be inside the function and immediately after the <cffunction> tag.

-Carl V.