• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Application.cfc, THIS.mappings & cfinclude

Explorer ,
Apr 05, 2012 Apr 05, 2012

Copy link to clipboard

Copied

hi there

In my Application.cfc

<cfcomponent>

  <cfscript>

    THIS.name = "myName";

    {etc.}

    THIS.mappings = StructNew();

    mappingname = "/include";

    mappingpath = "Z:\webincludes\didi\test\aaisession\appcfc";

    THIS.mappings[mappingname] = mappingpath;

  </cfscript>

  

  <cfinclude template="/include/appcfc-onApplicationStart.inc" >

    {etc.}

does not work!

When I do the mapping in CFadmin, it works. But I need to do it on an application base.

Is this due to the fact, that at the time CF tries to resolve the path of the template, the mapping has not yet been assigned?

How can I overcome this?

-Didi

TOPICS
Server administration

Views

8.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

You can do it in Application.cfc. However, you first have to go to the page Server Settings => Settings in the administrator, and check the option 'Enable Per Application Settings'.

On a separate note, what's the deal with the INC extension in the Application file? This might force the browser to display or download. It is best practice to avoid such presentation code in the application file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Hi BKBK

not searching for eggs at these days  ?

'Enable Per Application Settings' ist checked.

Believe me (at least on my server ..)

    <cfinclude template="/include/appcfc/appcfc-onApplicationEnd.inc" >

is not working as long as the <cfinclude ..> is in the 'outer space' of the Application.cfc.

The idea is to implement the events inside includes.

On the other hand:

  <cffunction name="onApplicationEnd" >

    <cfinclude template="/include/appcfc/appcfc-onApplicationEnd.inc" >

  </cffunction>

where I declare the event in the 'outer space' of the Application.cfc and I only put the body of the event of the include, it works.

So I think my theory gets more meat on the bone.

-----

now to your note of accessing the include:

Why not an INC extension to viualize the difference between a directly called CFM page and an include?

It just makes it more visible ...

  http://server/include/appcfc-onApplicationStart.inc


doesn't expose the include file. The mapping is CF internal only - Apache does not know about this.

That's why I took it outside the webroot.

  Z:\webincludes

is parallel to

    Z:\webroot

-----

our goal is structuring code and not putting all in one huge Application.cfc, since out onXxxStart events are quite long.

How would you do that?

-Didi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

OK. It just might be that ColdFusion isn't happy that the mapping is at once being set (written) and used (read) in the pseudo-constructor. What if you move the cfinclude tag to onRequestStart or, perhaps more appropriately, to onApplicationStart? Then it will run only after Application.cfc has been instantiated.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

BKBK:

The <cfinclude> is including the onApplicationStart() handler.  So kinda needs to be included from the body of Application.cfc.

I suspect this won't work for the very reason the OP suspects.

Didi:

The way to overcome it is to not factor your onApplicationStart handler out into an included file.  Why are you doing that?

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Adam Cameron. wrote:

BKBK:

The <cfinclude> is including the onApplicationStart() handler.

Does it? Sorry, I missed that. Likely because I don't quite understand Didi's passage about inner/outer space.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

BKBK:

The <cfinclude> is including the onApplicationStart() handler.

Does it? Sorry, I missed that. Likely because I don't quite understand Didi's passage about inner/outer space.

It was in the file name: appcfc-onApplicationStart.inc.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Adam Cameron. wrote:

BKBK:

The <cfinclude> is including the onApplicationStart() handler.  So kinda needs to be included from the body of Application.cfc.

There's the catch ...

What I do now is defining all events in Application.cfc and puttin all the code inside the events in a include, eg:

<cffunction name="onApplicationStart">

  <cfinclude template="/include/appcfc-onApplicationStart.inc" >

</cffunction>

That works fine.

Adam:

Why do I do this?

  • My application uses Shibboleth for authentication and I need a session mgt that is a little bit more sophisticated than the CF standard one. So my onApplicationStart,onSessionStart and onRequestStart count together about 200 lines of code just for authentication.
  • We will finally be 4 developpers at the same application.

Modularization of the code helps us to keep oversight and avoids interfering with each other.

Well, we are open for better stategies at any time 🙂

-Didi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

sorry for space confusing, to be more precise on that:

<cfcomponent>

  <cfscript>

    THIS.name = "myName";

    {etc.}

    THIS.mappings = StructNew();

    mappingname = "/include";

    mappingpath = "Z:\webincludes\didi\test\aaisession\appcfc";

    THIS.mappings[mappingname] = mappingpath;

  </cfscript>

  

  <cffunction name="onApplicationStart">

    <cfinclude template="/include/appcfc-onApplicationStart.inc" >

  </cffunction>

   {etc.}


</cfcomponent>

That way it works.

-Didi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

To make sure I get it now. This works:

<cffunction name="onApplicationStart">

    <cfinclude template="/include/appcfc-onApplicationStart.inc" >

  </cffunction>

But replacing this code with

<cfinclude template="/include/appcfc-onApplicationStart.inc" >

where the file appcfc-onApplicationStart.inc includes the event onApplicationStart, doesn't work. Am I there, Didi?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Version 1 (working):

Application.cfc contains:

...

<cffunction name="onApplicationStart">

    <cfinclude template="/include/appcfc-onApplicationStart.inc" >

</cffunction>

...

appcfc-onApplicationStart.inc contains:

<cset something = "example code that run in that event" >

-----------------------------------------------------------------------------------------------------------------------------------

Version 2 (NOT working):

Application.cfc contains :

...

<cfinclude template="/include/appcfc-onApplicationStart.inc" >

...

appcfc-onApplicationStart.inc contains:

<cffunction name="onApplicationStart">

    <cset something = "example code that run in that event" >

</cffunction>

-----------------------------------------------------------------------------------------------------------------------------------

-Didi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Didi wrote:

Version 1 (working):

Application.cfc contains:

...

<cffunction name="onApplicationStart">

    <cfinclude template="/include/appcfc-onApplicationStart.inc" >

</cffunction>

...

appcfc-onApplicationStart.inc contains:

<cset something = "example code that run in that event" >

---------------------------------------------------------------------- -------------------------------------------------------------

Version 2 (NOT working):

Application.cfc contains :

...

<cfinclude template="/include/appcfc-onApplicationStart.inc" >

...

appcfc-onApplicationStart.inc contains:

<cffunction name="onApplicationStart">

    <cset something = "example code that run in that event" >

</cffunction>

--------------------------------------------------------------------- --------------------------------------------------------------

Ah! Then it confirms my initial instinct that ColdFusion might not be happy that the mapping is at once being written and read in the pseudo-constructor.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Why do I do this?

  • My application uses Shibboleth for authentication and I need a session mgt that is a little bit more sophisticated than the CF standard one. So my onApplicationStart,onSessionStart and onRequestStart count together about 200 lines of code just for authentication.
  • We will finally be 4 developpers at the same application.

Modularization of the code helps us to keep oversight and avoids interfering with each other.

Well, we are open for better stategies at any time 🙂

It doesn't to me sound like you're using source control..?

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Adam Cameron. wrote:

It doesn't to me sound like you're using source control..?

--

Adam

We use CFBuilder2.

I have asked in a different thread for a recommendation - http://forums.adobe.com/message/4216531#4216531 -

I am still open for good ideas ..

-Didi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

It doesn't to me sound like you're using source control..?

We use CFBuilder2.

I have asked in a different thread for a recommendation - http://forums.adobe.com/message/4216531#4216531 -

CFB is a text editor, it's got nothing to do with source control.

Source control:

http://en.wikipedia.org/wiki/Revision_control

Specific examples:

http://en.wikipedia.org/wiki/Subversion_(software)

http://en.wikipedia.org/wiki/Git_(software)

You should not be developing software without using source control.  Especially if your lack of source control causes you to write code like you're suggesting to mitigate issues arising from lack of source control.

Lack of source control in your development cycle is your problem here.  Not how Application.cfc implements its mappings.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Adam Cameron. wrote:

CFB is a text editor, it's got nothing to do with source control.

  Adam

Sure!

What product would be your advice as a good integration with CFB2?

-Didi

EDIT: source control or not: I do not like those 789-line templates. So I would break it up also when using source control. But I definitely agree: as soon as I think I got the right combination CFB2 plus SC, I will use it!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

CFB is a text editor, it's got nothing to do with source control.

Sure!

What product would be your advice as a good integration with CFB2?

I can't speak for integration with CFB (or Eclipse for that matter), because I don't bother to try to integrate them.  Subversion has Eclipse plug-ins like Subverse and Subclipse, but I have found them both wanting compared to simply using TortoiseSVN via Window Explorer.

I also can't speak for Git, as I have not used it.  I understand it solves a few things that people perceive to be suboptimal with Subversion.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Didi wrote:

Adam Cameron. wrote:

CFB is a text editor, it's got nothing to do with source control.

  Adam

Sure!

What product would be your advice as a good integration with CFB2?

-Didi

EDIT: source control or not: I do not like those 789-line templates. So I would break it up also when using source control. But I definitely agree: as soon as I think I got the right combination CFB2 plus SC, I will use it!!!

Didi, you do indeed need Version Control software. One word in your other thread says it all: configuration. To avoid confusion, I will give my suggestions on version control in that thread.

In my opinion, the problem you're facing in this thread has less to do with version control, and more to do with information-hiding. That is, your code requires more information-hiding. That points to design.

You have to break the large pieces of code up into smaller modules. The logic should be to separate the code into public and private parts. The more code you shift into the private parts, that is, the more information you hide, the better will be your design.

A simple example to hide code in private modules is as follows:

<cffunction name="onApplicationStart">

<!--- call someFunction() to do some business when the application starts --->

<!--- call someOtherFunction() to do some other business when the application starts --->

</cffunction>

<cffunction name="someFunction" access="private">

<!--- business code --->

</cffunction>

<cffunction name="someOtherFunction" access="private">

<!--- business code --->

</cffunction>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

BKBK:

I 100% agree with you!

What I actually did not mention above: besides the 200 lines of code in the Application.cfc and the included events,

there is another include containing 8 UDFs with another 300 lines of code .

But your are right, I should scope most of them as private ... (they are all public yet).

What are you using for version control?

-Didi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Regarding:

In my Application.cfc

<cfcomponent>

     <cfinclude template="/include/appcfc-onApplicationStart.inc" >

I find that strange.  Why would you not have an onApplicationStart method in your cfc?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2012 Apr 08, 2012

Copy link to clipboard

Copied

Dan:

I just wanted to get a lean Application.cfc, since I do not like to scroll around in codefiles having hundreds of lines.

I prefer smaller code chunks - that's actually all behind it

Now it looks the way as you see below ...

BTW: Do you work with CFB2? What version control system are you using?

-Didi

___________________________________________________________________________

<cfcomponent>

  <cfscript>

    THIS.name = "myName";

    {etc.}

    THIS.mappings = StructNew();

    mappingname = "/include";

    mappingpath = "Z:\webincludes-outside-webroot\appcfc";

    THIS.mappings[mappingname] = mappingpath;

  </cfscript>

  <cffunction name="onApplicationStart">

    <cfinclude template="/include/appcfc-onApplicationStart.inc" >

  </cffunction>

  <cffunction name="onApplicationEnd" >

    <cfinclude template="/include/appcfc/appcfc-onApplicationEnd.inc" >

  </cffunction>

  <cffunction name="onSessionStart" >

    <cfinclude template="/include/appcfc/appcfc-onSessionStart.inc" >

  </cffunction>

  <cffunction name="onSessionEnd" >

    <cfargument name="SessionScope" required=true />

    <cfinclude template="/include/appcfc/appcfc-onSessionEnd.inc" >

  </cffunction>

  <cffunction name="onRequestStart" >

    <cfinclude template="/include/appcfc/appcfc-onRequestStart.inc" >

  </cffunction>

  <cffunction name="onRequestEnd" >

    <cfinclude template="/include/appcfc/appcfc-onRequestEnd.inc" >

  </cffunction>

</cfcomponent>

<!--- ***** END of Application.cfc ***** --->

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 09, 2012 Apr 09, 2012

Copy link to clipboard

Copied

I'm not sure if this has been mentioned, but I'd extend the Application.cfc rather than doing includes. It's not necessarily better or worse, but I think it makes more sense programmatically. Like so:

[ApplicationBase.cfc]

<cfcomponent>

  [onApplicationStart, other CF methods]

</cfcomponent>

[Application.cfc]

<cfcomponent extends="ApplicationBase">

  [your own business methods]

</cfcomponent>

They can also extend each other, so you could have Application.cfc extending ApplicationBusinessMethods.cfc which in turn extends ApplicationCoreMethods.cfc which extends ApplicationBase.cfc, which has the built-in CF methods.

Just another little something to throw into the mix there

O.

PS: CF Builder 2, VisualSVN Server and TortoiseSVN

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 09, 2012 Apr 09, 2012

Copy link to clipboard

Copied

I'm not sure if this has been mentioned, but I'd extend the Application.cfc rather than doing includes. It's not necessarily better or worse, but I think it makes more sense programmatically. Like so:

Yep, using inheritance would be preferable to using includes in the way Didi is currently doing it, although it's kinda contorting the notion of inheritance a bit.  However just not doing either would be better here, I think.

I could possibly see doing something like this as passable:

// ApplicationEventHandlers.cfc

component {

          this.name = "myApp";

          // etc

          function onApplicationStart(){

 

          }

 

          function onApplicationEnd(){

 

          }

}

// SessionEventHandlers.cfc

component extends="ApplicationEventHandlers" {

          function onSessionStart(){

 

          }

 

          function onSessionEnd(){

 

          }

}

// RequestEventHandlers.cfc

component extends="SessionEventHandlers" {

          function onRequestStart(){

 

          }

          function onRequest(){

 

          }

          function onMissingTemplate(){

 

          }

          function onError(){

 

          }

 

          function onRequestEnd(){

 

          }

}

However this is abusing the notion of inheritance I think.  And as per my previous post, basing one's architecture on developer-laziness is not something I'd engage in.  I'd just smarten-up my approach to my work practices/ethics.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 09, 2012 Apr 09, 2012

Copy link to clipboard

Copied

Ooh, ooh, I've got another one now I've realised what you're probably doing wrong.

Agreed no programming decision should ever be made on the aspect solely of asthetics, and in fact Partial Classes (the ability to spread a class definition over many files then having them compiled into one) is one of the many reasons I like C#.

I'd guess your methods are loosely grouped based on function - move each of these out into a separate class, then create an instance in the Application scope. For example:

[SecurityFunctions.cfc]

<cfcomponent name="SecurityFunctions">

     [security-based methods]

</cfcomponent>

[Application.cfc]

<cfcomponent>

     [onApplicationStart]

     <cfset Application.SecurityFunctions = new SecurityFunctions() />

</cfcomponent>

You can then call Application.SecurityFunctions.MyMethod() publicly, or stick it in the variables scope if you'd rather it be private.

No bodging of code, just nice neat class usage. This is how I achieve what you're trying to.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 09, 2012 Apr 09, 2012

Copy link to clipboard

Copied

Yeah, that's better than the inappropriate use of inheritance.

It still leaves the main event handlers in the Application.cfc though... so it depends on whether the bulk of the code is in them or the support methods.

However the code in the event handlers can probably be refactored into smaller / more purposeful methods though.

Difficult to say without:

a) seeing all the code;

b) having more motivation to refactor someone else's code than I currently do.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation