7

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 (i.e. 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>
RyanCJI
  • 464
  • 2
  • 8
  • 3
    Need more of the App.cfc code to give a proper answer. If you want to replicate the App.cfm functionality, I would place your variable declaration in the onRequestStart function. You may need to place it in the request scope. You could also use the onRequest function, but that comes with other considerations. – Sean Coyne Mar 14 '14 at 19:13
  • Thanks Sean. I tried using onRequestStart, but it still did not work. But, let me try again and restart the service. – RyanCJI Mar 14 '14 at 19:17
  • @rcjames give this a read. http://www.bennadel.com/blog/726-ColdFusion-Application-cfc-Tutorial-And-Application-cfc-Reference.htm – Cory Fail Mar 14 '14 at 19:20
  • @rcjames like I mentioned, with onRequestStart you can't use the variables scope directly, you can set it to request.companylogotext then reference it the same way in other pages. If you need it in the variables scope then you'll have to use onRequest() but again, that comes with other problems with certain requests. FWIW, you probably should be setting it as an application level variable (unless you have to use it differently, or it changes from request to request). if it doesnt change, just set it once in the application scope. – Sean Coyne Mar 14 '14 at 19:30
  • 1
    I'd just like to add that compared to a lot of CFML questions around here, this one was very well presented. Thanks (I say that as I find trying to help ppl here a bit soul-destroying sometimes). – Adam Cameron Mar 14 '14 at 19:53

3 Answers3

4

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

e.g.

<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.

Henry
  • 32,689
  • 19
  • 120
  • 221
  • Thanks Henry. Are you saying I should put `` in the OnRequestStart function contained in `Application.cfc`? – RyanCJI Mar 14 '14 at 19:20
0

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

Cory Fail
  • 1,070
  • 8
  • 26
0

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 ...