Variable scope in CF 10 versus CF MX7

I am running into a weird issue with my ColdFusion 10 code. I am new to ColdFusion, so go easy on me. The reason it is weird is because it does not seem to occur in older versions of this platform (ie MX 7).

A little info first:

I have two environments. A ColdFusion 10 and a ColdFusion MX 7 (IIS 7 and IIS 5, respectively). In the ColdFusion 10 environment, I have an Application.cfc file with the following statement...

<cfset CompanyLogoText = "Acme Company">

This Application.cfc file is in the web root (mydomain.com). I also have a CFM file in a sub folder of the web root at mydomain.com/pages/default.cfm . It contains the following markup...

<cfoutput><p>#CompanyLogoText#</p></cfoutput>

The issue

When I navigate to mydomain.com/pages/default.cfm , I get an error from coldfusion. The error is "Variable COMPANYLOGOTEXT is undefined."

The weird part

I am not getting this error in the ColdFusion MX 7. The only difference is that the CF MX 7 environment uses a Application.cfm file, but with the same exact line.

Question

How can I get the pages/default.cfm file to see my variable CompanyLogoText in the CF 10 environment?

Here is the full markup

Application.cfc

<cfcomponent>
<cfset This.name = "test_cf">
<cfset This.Sessionmanagement="yes">
<cfset This.Sessiontimeout="#createtimespan(0,0,10,0)#">
<cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">
<cfset This.setclientcookies="no" >
<cfset This.clientmanagement="no">


<cffunction name="onApplicationStart">

    <cfset CompanyLogoText = "Acme Company">    

</cffunction>



<cffunction name="onRequestStart">
    <cfargument name="requestname" required=true />
    <cfset CompanyLogoText = "Acme Company">    


</cffunction>


</cfcomponent>

Pages/Default.cfm

<cftry>

<cfoutput><p>#CompanyLogoText#</p></cfoutput>

<cfcatch>

<p>Could not read CompanyLogoText<br/><br/>

<cfoutput>
<br/>Message:  #cfcatch.message#
<br/>Details:  #cfcatch.detail#.

</cfoutput>

</cfcatch>

</cftry>

That's the difference between Application.cfm and Application.cfc

Use onRequest() , set the variables, then cfinclude the target file. That's the only way to share the variables scope.

https://wikidocs.adobe.com/wiki/display/coldfusionen/onRequest

eg

<cffunction name="onRequest" returnType="void"> 
    <cfargument name="targetPage" type="String" required=true/> 

    <cfinclude template="globalVars.cfm">

    <cfset variables.foo = "bar">

    <cfinclude template="#Arguments.targetPage#">
</cffunction>

QUOTE: CF8: Migrating from Application.cfm to Application.cfc

Put in the onRequest method any code that sets Variables scope variables and add a cfinclude tag that includes the page specified by the method's Arguments.Targetpage variable.


As mentioned your application.cfc needs to be formatted correctly. Your best bet is to give this a read and format your .cfc accordingly.

http://www.bennadel.com/blog/726-ColdFusion-Application-cfc-Tutorial-And-Application-cfc-Reference.htm


Don't see an answer marked yet. If you have an application.cfm file in the sub-directory it will override the application.cfc in the root. Just a possibility ...

链接地址: http://www.djcxy.com/p/78278.html

上一篇: OSX:PHP错误localhost并无法加载动态库

下一篇: CF 10中的可变范围与CF MX7相比