views:

416

answers:

2

hi everyone, i was blocked by a coldfusion problem, any suggestions are appreciated. now lemme decribe my problem.


i have an Application.cfc in my website root, the content in it is as follows:

<cfcomponent output="false">
    <cffunction name="onRequest" returnType="void">
     <cfargument name="thePage" type="string" required="true">
     <cfinclude template="#arguments.thePage#">
    </cffunction>
</cfcomponent>

and also i have a cfm template of which the name is test.cfm, it's content is listed as follows:

<cfdump var="#variables.this#"><br /><br /><br /><br /><br /><br />
<cfdump var="#this#">

now if you request the test.cfm, everything is ok, but when i delete the onRequest method in Application.cfc and request test.cfm again, it complaints that "Element THIS is undefined in VARIABLES. ", i don't know why, can anybody explain it? great thanks.

ps:

you can add as many functions into Application.cfc, such as onSessionStart, onSessionEnd, onApplicationStart, onApplicationEnd..., but if there is not a onRequest method, you request test.cfm and get error. i just don't know why.

+7  A: 

It's because the this scope refers to a cfc instance. When you include test.cfm from within application.cfc this refers to the application.cfc instance. When you call test.cfm directly this does not exist because the request did not go through application.cfc, so you're not inside a cfc instance.

Not sure what you were trying to do, but you probably don't want to use this outside of a cfc. If you want to dump the application scope from test.cfm just do this instead:

<cfdump var="#application#"/>
dwb
+3  A: 

Returning true from the onRequestStart method will load the page for you. As dwb stated your 'this' is referring to to the Application.cfc because you have included it from within one of the methods. If you need to refer to the Application use the application scope not 'this', unless you really are inside of the Application.cfc.

Nick
This is incorrect -- and shame on anyone who blindly upvoted without testing or knowing for sure that it was correct! If you don't include the requested file, you will see no output. (See: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=AppEvents_08.html) Perhaps you were thinking of the onRequestEnd event? What you said is true for onRequestStart. (See: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=AppEvents_08.html)
Adam Tuttle
Ha- you are correct. I was thinking of onRequestStart. I'll edit my answer although I'm not sure it is still relevant...
Nick