12 Replies Latest reply on Oct 16, 2012 11:08 AM by Adam Cameron.

    Using application.cfc instead of application.cfm

    plarts Level 1

      I just discover the role of application.cfc

      seems very interesting.

      All my applications are using an application.cfm template.

       

      I have seen in the doc the migration from application.cfm to application.cfc,

      but I do not understand.

       

      Is the application.cfc running instead of application.cfm ?

       

      With the application.cfc , I have a blank page, no error messages, I do not know what to do.

      why it does not go to the index.cfm as it did with application.cfm

       

      If I take all the content of my current application.cfm

      where can I put it in the application.cfc ?

      I tried in : 

      <cffunction name="OnRequestStart"

       

      but no thing happended.

       

       

      thanks for help with this major change. (I am running CF8).

      Pierre.

        • 1. Re: Using application.cfc instead of application.cfm
          Dan Bracuk Level 5

          If I had a perfectly good application running with Application.cfm I would not change it.  Having said that, if I did have a reason to change it, I would put stuff in the functions where I want them to run.

           

          If you google, "adam cameron coldfusion" you'll see a link to a blog he has written.  Somewhere in that blog is some guidance on how to use the functions in Application,.cfc.

          • 2. Re: Using application.cfc instead of application.cfm
            plarts Level 1

            Thanks for your reply,

             

            I would like to use application.cfc  to use the "OnSessionEnd()" function.

             

            Also in me old application, moving from application.cfm to application.cfc,

            I get a blank page.  Debugging :

             

            In the CFC  OnApplicationStart , I put :

            <cfset pres_datasource="A01_onvide">  (the datasource)

             

            And when doing a query on this datasource, the datasource variable is "blank",

            no error message.

             

            Any idea ?

            thanks.

             

            I will look at : adam cameron coldfusion blog

             

            thanks for your help.

            Pierre.

            • 3. Re: Using application.cfc instead of application.cfm
              Miguel-F Level 3

              Even though you have placed the setting of your variable in OnApplicationStart you still need to specify the application scope.

               

              Change the statement in your Application.cfc to:

               

              <cfset application.pres_datasource="A01_onvide">

               

              And then in your code, reference the variable like this:

               

              <cfquery name="qname" datasource="#application.pres_datasource#" ... >

               

               

              By the way, that should be the same way it was done in application.cfm

               

               

              If you are not wanting to set the variable in the application scope for some reason then you could move your statement as it is (without specifying the application scope) to the OnRequestStart method instead.  That method runs on every request so the variable would be set on every request.  Then you would not need to change your cfquery references.  Just another way...  For what it's worth, I always set my datasources in the application scope.

              • 4. Re: Using application.cfc instead of application.cfm
                plarts Level 1

                I did so :

                <cfset application.pres_datasource="A01_onvide">

                in OnApplicationStrart or  OnRequestStart

                 

                and did so :

                <cfquery name="qname" datasource="#application.pres_datasource#" ... >

                 

                Restart the CF service

                 

                And I still have no datasource recognised in the 2 cases.

                 

                Any idea ?

                 

                Thanks Pierre.

                • 5. Re: Using application.cfc instead of application.cfm
                  Dan Bracuk Level 5

                  When in doubt, look at your data.  cfdump the application scope and see if the variable is there.

                   

                  By the way, the fastest way to make onApplicationStart run after you change it is to also change the name of the application.  Keep it simple, just add a number to the end of it.

                  • 6. Re: Using application.cfc instead of application.cfm
                    plarts Level 1

                    I tried this :

                    <cffunction
                      name="OnRequestStart"
                      access="public"
                      returntype="boolean"
                      output="false"
                      hint="Fires at first part of page processing.">

                     

                     

                      <cfset application.dsn = structNew()>

                      <cfset application.dsn.pres_datasource="A01_onvide">
                      <cfset application.dsn.forum_datasource="A01_forum_onvide">
                      <cfset application.dsn.user_datasource="A01_user_onvide">
                     
                      <!--- Return out. --->
                      <cfreturn true />
                     
                    </cffunction>

                     

                    Cfdump is OK

                    and datasource is OK now.

                     

                    Query is :

                    <cfquery name="get_module" datasource="#application.dsn.pres_datasource#">

                      select * from sys_module

                    </cfquery>

                     

                    Thanks, any comments,

                    I need to change all my datasource names in the application.

                    • 7. Re: Using application.cfc instead of application.cfm
                      Dan Bracuk Level 5

                      Since you invited comments:

                       

                      Setting application variables is onRequestStart is a bad idea.  In your case where you never change the value it's redundant coding that slows down production.  If values were being changed, you could have user2 messing up user1.

                       

                      I like simplicity.  For this reason, I would have simply set 3 variables for the datasources as opposed to a structure with 3 keys.

                      • 8. Re: Using application.cfc instead of application.cfm
                        plarts Level 1

                        Thanks a lot for suggestion,

                        I just put :

                        <cfset pres_datasource="A01_onvide">

                          <cfset forum_datasource="A01_forum_onvide">

                          <cfset user_datasource="A01_user_onvide">

                        in OnRequestStart

                         

                        and it does work.

                        then no change in all files.

                         

                        Is rthat the good place to put that code : OnRequestStart  ?

                        • 9. Re: Using application.cfc instead of application.cfm
                          Miguel-F Level 3

                          +1 on Dan's comments.

                           

                          There is nothing wrong with setting those variables in onRequestStart, however as I mentioned before, when I have variables that are used by an entire application I set them in the application scope.

                           

                          Here is the difference, when you set them as you are in onRequestStart those three lines of code and the memory space they utilize will be run on EVERY page request.  Not a big deal, ColdFusion will handle it and handle it quickly.  If you set the three variables in onApplicationStart however those three lines of code will only be run on the first request after ColdFusion is started or after the application expires.  Then those variables and their memory are allocated for the duration of your application. 

                           

                          I won't say that one is the right way and the other is the wrong way.  Both ways will absolutely work.  I just prefer to utilize the application scope for scenarios such as this.

                           

                           

                          I will add that for you, in this current situation, it might make more sense to just use the onRequestStart method because it saves you from modifying all of the other code.

                           

                          Message was edited by: Miguel-F

                          • 10. Re: Using application.cfc instead of application.cfm
                            plarts Level 1

                            Ok,

                            So if I put it in OnApplicationStart

                            I need to change my variables names to application.xxxxxxxxxxxx

                             

                            that's right ?

                            • 11. Re: Using application.cfc instead of application.cfm
                              Miguel-F Level 3

                              No.  In order to put the variables into the application scope you need to rename them application.xxxxxxxxx  In order to set those variables one time when the application starts you need to put them in onApplicationStart.  Then, yes, you would need to change all of your references to those variables to include the application prefix.

                               

                              Maybe this will help you: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=appFramework_03.html

                              • 12. Re: Using application.cfc instead of application.cfm
                                Adam Cameron. Level 5

                                I agree  that applicaiton-scoped variables should be set in onApplicationStart(), not onRequestStart(), but I strongly disagree that not using a substruct to "namespace" your DSNs is a good idea.

                                 

                                IE, do this:

                                <cfset application.dsn = {

                                          pres_datasource                    = "A01_onvide",

                                          forum_datasource          = "A01_forum_onvide",

                                          user_datasource                    = "A01_user_onvide"

                                }>

                                 

                                And keeping your DSNs together in one place is tidier than having separate seemingly unrelated variables floating around in the application scope.

                                 

                                I might pluralise the top level variable though: it's a bunch of DSNs, not one DSN.

                                 

                                --

                                Adam