ColdFusion Application.cfc - order of execution

前端 未结 5 551
旧巷少年郎
旧巷少年郎 2021-01-05 00:34

I need a reality check - and hopefully an explanation (if my reality is wrong).

The way the CF application framework evaluates things is this (my understanding) - re

相关标签:
5条回答
  • 2021-01-05 01:02

    There's two things at play here: when code runs, and when variable scopes are availed and how long they last.

    • The code outwith any method (ie: the "pseudoconstructor") runs every request. Obviously minimise the amount of code in this part of the CFC.
    • Code in various event handlers run as indicated by the event handler name, eg: onApplicationStart() code runs only once, when the application starts. Ditto onSessionStart() only runs once per new session.

    Scopes:

    • this scope is available throughout the CFC. Behaves exactly like the this-scope in any other CFC, except some this-scoped variables have special meaning (like this.name, this.datasource, etc). Those special-meaning variables can be changed per session or per request in the relevant handlers, but seem to apply to the entire system (ie: not for the specific session or request making the setting change). In normal CFCs, the this scope is used to expose public variables, but as there is no public instance of Application.cfc, there is no point using the this scope beyond making those special settings. If one wants to have variables available to all methods within the CFC, use the variables scope as one normally would. Contrary to someone's advice here, this-scoped variables are NOT the same as application-scoped variables.
    • The request scope is also available throughout the CFC (pseudo constructor and methods). These will also be available to the calling code of templates called later in the request, like any other request-scoped variables.
    • Application scope: not available in the pseudo-constructor, even after the this.name setting is made. Only becomes available in onApplicationStart() and thenceforth from there.
      • Session scope: similarly, not available in the pseudo-constructor or onApplicationStart()l and not until onSessionStart().

    I've demonstrated this in a blog post (test code provided), over here. it's too lengthy to include here, but the stuff above summarises it.

    0 讨论(0)
  • 2021-01-05 01:05

    The ColdFusion Application.cfc documentation has this tidbit of knowledge:

    When a request executes, ColdFusion runs the CFC methods in the following order:

    1. onApplicationStart (if not run before for this application)
    2. onSessionStart (if not run before for this session)
    3. onRequestStart
    4. onRequest/onCFCRequest
    5. onRequestEnd

    The onApplicationEnd, onSessionEnd, and onError CFCs are triggered by specific events.

    The overall request order has (at least) two more steps.

    0: execute all code in cfcomponent that isn't in a cffunction
    0.5: run the equivalent of the cfapplication tag for creating the Application

    As such the answers to your questions are:

    1. If you're setting those variables in step 0, then yes.
    2. Correct.
    3. That depends on where you're setting the variable. If the values that you are trying to change are listed on the Application variables documentation page for Application.cfc then they must be in step 0. Setting them elsewhere will update the this scope, but will not take effect in step 0.5.
    0 讨论(0)
  • 2021-01-05 01:06

    Please see the comments as it would appear the below post works but it does not. If you dump the this scope, your new value is displayed, but it does not actually change any application settings.

    You can change any of the application settings anywhere you like; however, because the pseudo constructor runs each time a page is requested you will need to constantly change the setting after the pseudo constructor runs. The application scope is not available in the pseudo constructor so you can do this in the onRequestStart or onRequest function. I have done a simple test reassigning the customtagpaths per a condition in the onRequestStart function. You will notice the first time you access the page the custom tags folder will be 'customtags' additional requests will indicate 'someOtherCustomtagsFolder' On a side note if your application settings change on a per-user basis your global application settings will be flip-flopping and could cause problems with other users getting incorrect settings.

    <cfcomponent>
    <!--- pseudo constructor --->
    <cfset this.customtagpaths = expandPath('./customtags')>
    
    <!--- onRequestStart --->
    <cffunction name = "onRequestStart" returnType="void">
         <cfif structKeyExists(application,'testSetting')>
              <cfset this.customtagpaths = expandPath('./someOtherCustomtagsFolder')>
         </cfif>
    </cffunction>
    
    <!--- onRequest --->
    <cffunction name = "onRequest" returntype="void">
         <cfargument name="targetPage" type="String" required = "true" />
         <cfdump var = "#this#" label = "this">
         <cfset application.testSetting = "foo">
         <cfinclude template="#Arguments.targetPage#">
    </cffunction>
    </cfcomponent>
    
    0 讨论(0)
  • 2021-01-05 01:07

    Anything in the this scope inside an Application.cfc file becomes an application varialbe and is only created ONCE per application life cycle. After the application starts, there is no other user for this within Application.cfc.

    The first time a CF application is run, the contents of onApplicationStart() are run before onRequest/Start/End (with the exception of the "new in CF10" onServerStart()).

    Any application variables set anywhere in the application exist until the application is stopped.

    Your code from #3 should just be

    if ( !structKeyExists( application, "myvar" ) { application.myvar = foo; }

    then reference application.myvar wherever you need it.

    From your description, nothing needs to be added to the this scope, it just needs to be put in the application scope.

    0 讨论(0)
  • 2021-01-05 01:25

    the application scope is not available in the Application.cfc pseudo constructor, because until this.name has been set there is no way to tie the request to the application.

    If you are worried about the overhead of creating your apps mappings, one approach would be to cache them in the server scope which is available.

    if(!structkeyexists(server, 'myappmappings')){ server.myappmappings = createMappings(); } this.mappings = server.myappmappings;

    you might also be able to use cachePut/cache Get to store the mappings in ehcache, but I've not tried that in the pseudo constructor.

    0 讨论(0)
提交回复
热议问题