12 Replies Latest reply on Dec 1, 2012 10:50 AM by BKBK

    Sharing application scope

    wannab0133 Level 1

      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?

        • 1. Re: Sharing application scope
          BKBK Adobe Community Professional & MVP

          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.

          • 2. Re: Sharing application scope
            wannab0133 Level 1

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

            Variable APPLICATION is undefined.

            • 3. Re: Sharing application scope
              Miguel-F Level 3

              Have you enabled session management in your Application.cfc?

              • 4. Re: Sharing application scope
                BKBK Adobe Community Professional & MVP

                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.

                • 5. Re: Sharing application scope
                  Miguel-F Level 3

                  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.

                  • 6. Re: Sharing application scope
                    BKBK Adobe Community Professional & MVP

                    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>

                    • 7. Re: Sharing application scope
                      wannab0133 Level 1

                      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>

                      • 8. Re: Sharing application scope
                        BKBK Adobe Community Professional & MVP

                        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" />

                        • 9. Re: Sharing application scope
                          wannab0133 Level 1

                          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.

                          • 10. Re: Sharing application scope
                            BKBK Adobe Community Professional & MVP

                            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.

                            • 11. Re: Sharing application scope
                              wannab0133 Level 1

                              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.

                              • 12. Re: Sharing application scope
                                BKBK Adobe Community Professional & MVP

                                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.