Skip to main content
Known Participant
November 28, 2012
Question

Sharing application scope

  • November 28, 2012
  • 1 reply
  • 2378 views

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?

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    November 28, 2012

    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.

    Known Participant
    November 28, 2012

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

    Variable APPLICATION is undefined.

    Miguel-F
    Inspiring
    November 28, 2012

    Have you enabled session management in your Application.cfc?