• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Sharing application scope

Community Beginner ,
Nov 27, 2012 Nov 27, 2012

Copy link to clipboard

Copied

This question has been asked in the past but not answered completely, so here is my issue:

I am currently working on a site that has two separate sections(one public section and another admin section), but need to share the same Application variables.

Basic file structure....

Root

-- Admin  -- Application.cfc  -- index.cfm 

-- Application.cfc  -- index.cfm  

The public side will contain the application start and end code and the the admin Application.cfc will include the public application.cfc(<cfinclude template="../Application.cfc" />) and the login logic.   The reason I am trying to do this is that my application scope needs to be accessible by both sides. My understanding is that the CF server will treat this all as one application since they have the same name.

During my testing, I set application.cfcroot variable in the public application.cfc.  That variable is not accessible in the admin subfolder.  My understanding is the fact that by including the original(public) application.cfc, the application.cfcroot variable will be accessible in subfolders(in this case: admin).  Th error I get is that the variable is undefined.  I test this by trying to dump the application right after I include it in my application.cfc located in the admin folder.  For example:

Contents of application.cfc in root:

-------------------------------------------

<cfset this.name="myapp" >

<cffunction name = "onApplicationStart">

     <cfset application.cfcroot = "myapp.cfcs.">

</cffunction>

-------------------------------------------

Contents of application.cfc in admin folder:

-------------------------------------------

<cfinclude template="../Application.cfc" />

<cfdump var="#application#" label="1" />

-------------------------------------------

error:

Variable APPLICATION is undefined.

Any ideas?

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

The tag cfinclude is meant to be used for including CFM files. For CFCs, use the extends attribute to enable one component to inherit the functionality of another. It goes like this

Contents of Application.cfc in root:

-------------------------------------------

<cfset this.name="myapp" >

<cffunction name = "onApplicationStart">

     <cfset application.cfcroot = "myapp.cfcs.">

</cffunction>

-------------------------------------------

Contents of Application.cfc in admin folder:

-------------------------------------------

<!--- I have used the dot notation and have assumed that the parent Application.cfc is located in the directory {WEB_ROOT}/myapp. If not, then you will have to modify accordingly --->

<cfcomponent extends="myapp.Application">

<cffunction name = "onApplicationStart">

     <!--- The keyword super stands for the parent Application component. Calling its onApplicationStart method will initialize the variable application.cfcroot --->

     <cfset super.onApplicationStart()>

     <cfset application.testAdminVar= "value of test admin var">

</cffunction>

</cfcomponent>

-----------------------------------------------------

Contents of /admin/testPage.cfm:

<cfdump var="#application#">

-------------------------------------------

Now, run the test page.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

I appreciate the reply.  However, I am still getting the same error:

Variable APPLICATION is undefined.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

Have you enabled session management in your Application.cfc?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

wannab0133 wrote:

I appreciate the reply.  However, I am still getting the same error:

Variable APPLICATION is undefined.

No way! Ok, try this as the admin Application.cfc:

<cfcomponent extends="myapp.Application">

<cfset this.applicationTimeout = "#createTimespan(1,0,0,0)#">

<cfset this.sessionManagement = "true">

<cfset this.sessionTimeout = "#createTimeSpan(0,0,20,0)#">

<cffunction name = "onApplicationStart">

     <!--- The keyword super stands for the parent Application component. Calling its onApplicationStart method will initialize the variable application.cfcroot --->

     <cfset super.onApplicationStart()>

     <cfset application.testAdminVar= "value of test admin var">

</cffunction>

</cfcomponent>

Oh, and save the file as Application.cfc, with capital A.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

BKBK wrote:

No way! Ok, try this as the admin Application.cfc:

The session management may need to be enabled in the root Application.cfc.  Right?

wannab0133 - it might help us if you could post your entire Application.cfc (both of them).  Assuming they are not too large.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

Miguel-F wrote:

BKBK wrote:

No way! Ok, try this as the admin Application.cfc:

The session management may need to be enabled in the root Application.cfc.  Right?

Yes, indeed. Not only session and not only in Application.cfc. The use of application and session variables should be enabled in the Coldfusion administrator. The main Application.cfc should also be at a minimum something like

<cfcomponent>

<cfset this.applicationTimeout = "#createTimespan(1,0,0,0)#">

<cfset this.sessionManagement = "true">

<cfset this.sessionTimeout = "#createTimeSpan(0,0,20,0)#">

<cfset this.name = "myapp">

<cffunction name = "onApplicationStart">

          <cfset application.cfcroot = "myapp.cfcs.">

</cffunction>

</cfcomponent>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

Session management is enabled as the rest of my app relies on sessions.  Also, application variables are enabled in the administrator.  I had no idea it was in there, but it is checked.  Here are my Application.cfc files:

Application.cfc (root/gemstartech directory)

<cfcomponent>

          <cfset this.name="gemstartech" >

          <cfset this.sessionmanagement=true >

          <cfset this.setclientcookies=true >

          <cfset this.setdomaincookies=false >

          <!---This sets timeout to be 20 minutes(days, hours, minutes, seconds)--->

          <cfset this.sessiontimeout="#CreateTimeSpan(0,0,20,0)#" >

   

          <cffunction name = "onApplicationStart">

        <cfset application.cfcroot = "gemstartech.cfcs.">

          </cffunction>

   

          <cffunction name = "onRequestStart">

                    <cfparam name="CGI.REMOTE_ADDR" default="">

       

                    <cfinclude template="shared/udf.cfm">

 

          </cffunction>

</cfcomponent>

Application.cfc (root/gemstartech/admin directory)

<cfcomponent extends="gemstartech.Application">

<cfset this.applicationTimeout = "#createTimespan(1,0,0,0)#">

<cfset this.sessionManagement = "true">

<cfset this.sessionTimeout = "#createTimeSpan(0,0,20,0)#">

<cffunction name = "onApplicationStart">

     <!--- The keyword super stands for the parent Application component. Calling its onApplicationStart method will initialize the variable application.cfcroot --->

     <cfset super.onApplicationStart()>

     <cfset application.testAdminVar= "aValue">

</cffunction>

<cfdump var="#application#" label="1" />

<!--- log user out if they click 'logout' --->

<cfif isDefined("url.logout") >

          <cfset structClear(session)>

          <cflogout>

          <cflocation url="index.cfm" addtoken="no">

</cfif>

</cfcomponent>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2012 Nov 28, 2012

Copy link to clipboard

Copied

Looks neat indeed. Well, except for two things. Firstly, the code block <cfdump ... </cfif> is somewhat stranded in the component. Secondly, as a general rule, Application.cfc shouldn't be used for display.

To solve those 2 issues, I would do something like this

Application.cfc (root/gemstartech/admin directory)

<cfcomponent extends="gemstartech.Application">

<cfset this.applicationTimeout = "#createTimespan(1,0,0,0)#">

<cfset this.sessionManagement = "true">

<cfset this.sessionTimeout = "#createTimeSpan(0,0,20,0)#">

<cffunction name = "onApplicationStart">

     <!--- The keyword super stands for the parent Application component. Calling its onApplicationStart method will initialize the variable application.cfcroot --->

     <cfset super.onApplicationStart()>

     <cfset application.testAdminVar= "aValue">

</cffunction>

<cffunction name="onRequestStart">

<cfargument name = "targetPage" type="String" required="true">

<!--- log user out if they click 'logout' --->

<cfif isDefined("url.logout") >

          <cfset structClear(session)>

          <cflogout>

          <cflocation url="index.cfm" addtoken="no">

</cfif>

<!--- Run the onRequestStart of the parent Application.cfc --->

<!---<cfset super.onRequestStart(arguments.targetPage)>--->

</cffunction>

</cfcomponent>

testpage.cfm (in root/gemstartech/admin directory)

<cfdump var="#application#" label="1" />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 01, 2012 Dec 01, 2012

Copy link to clipboard

Copied

BKBK,

Your code worked.  I was grateful.  I did not know why it worked, but I am using it.  Now, though I am adding one line to my application.cfc witihn the admin folder.  below is the code:

<cfcomponent extends="gemstartech.Application">

<cfset this.applicationTimeout = "#createTimespan(1,0,0,0)#">

<cfset this.sessionManagement = "true">

<cfset this.sessionTimeout = "#createTimeSpan(0,0,20,0)#">

<cffunction name = "onApplicationStart">

     <!--- The keyword super stands for the parent Application component. Calling its onApplicationStart method will initialize the variable application.cfcroot --->

     <cfset super.onApplicationStart()>

     <cfset application.testAdminVar= "aValue">

</cffunction>

<cffunction name="onRequestStart">

    <cfargument name = "targetPage" type="String" required="true">

    <!--- log user out if they click 'logout' --->

    <cfif isDefined("url.logout") >

        <cfset structClear(session)>

        <cflogout>

        <cflocation url="index.cfm" addtoken="no">

    </cfif>

</cffunction>

<!--- login validation --->

<cfinclude template="logmein.cfm">

       

</cfcomponent>

I put in bold the line I added.  It is just an include for the login logic.  I removed all the code in logmein.cfm and just added the following:

<cfdump var="#application#" label="2" />

error:

Variable APPLICATION is undefined.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 01, 2012 Dec 01, 2012

Copy link to clipboard

Copied

There is no rhyme or reason to include the file in Application.cfc. By the time you're including it Application.cfc has not yet completely run, hence the very application scope you wish to dump is as yet undefined.

As I said, to dump the application scope, run the code <cfdump var="#application#" label="2" /> in a CFM file that you save in the directory  root/gemstartech/admin.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 01, 2012 Dec 01, 2012

Copy link to clipboard

Copied

BKBK,

Thanks for the info.  I only dumped the var in the include b/c I was getting the error.  I got this to work finally.  All I did was take:

<!--- login validation --->

<cfinclude template="logmein.cfm">

And move it to the onRequestStart() of the same application.cfc.  logemin.cfm has the login logic and everything works now.  I appreciate all your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 01, 2012 Dec 01, 2012

Copy link to clipboard

Copied

LATEST

I agree. OnRequestStart() is a good place to run code in the context of a CFM page, especially code containing login logic. However, dumping application variables, for whaever reason, is best done in a separate CFM page.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation