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

Idiots guide to application.cfc

Participant ,
Aug 16, 2008 Aug 16, 2008
Hi all,
The code is my very simple application.cfm and onrequestend.cfm how do I combine them into an application.cfc? Thank you
TOPICS
Getting started
2.1K
Translate
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 ,
Aug 16, 2008 Aug 16, 2008
You can have a onRequestEnd function in your application.cfc.
Translate
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 ,
Aug 17, 2008 Aug 17, 2008
> The code is my very simple application.cfm and onrequestend.cfm how do I
> combine them into an application.cfc? Thank you

1) RTFM, which covers this:
http://livedocs.adobe.com/coldfusion/8/appFramework_03.html and specfically
this: http://livedocs.adobe.com/coldfusion/8/appFramework_15.html
2) Come back if - having done some background investigation - something is
still not clear.

This is a pretty simple requirement you have here, so just by following the
docs, you should get it sorted quickly.

It's always better in the first instance to try to help yourself, rather
than getting someone else to help you, because one learns more by DIY.

--
Adam
Translate
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
Participant ,
Aug 17, 2008 Aug 17, 2008
Thank you for those links Adam great stuff. Here's my attempt, it is a bit confusing about enabling session-what do you think of my attempt? Thanks
Translate
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 ,
Aug 17, 2008 Aug 17, 2008
The stuff that you want to run when the application starts should be in
onApplicationStart(), not onSessionStart().

Sometimes it takes a bit to work out exactly what processing relates to the
application, or only to a user's session, or onto to the given request. I
see a lot of code initialising session stuff in onRequestStart(), for
argument's sake (which is seldom the place ot be doing that sort of thing).

However for your situation, it's a given that stuff that sets
application-level settings (like those from the <cfapplication> tag) don't
get set on a session-by-session basis; and likewise variables in the
application-scope probably aren't supposed to be set at the start of each
session either. If it's got the word "application" in it, it's likely to
want to be initialised along with the rest of the application. Ditto
things that have "session" somewhere in the name are generally going to be
deal with by the session-specific event handlers; or "request" in the name
the request-specific event handlers.

I don't think you read the bullet points (especially the first two) on the
first link I sent you very carefully. I also don't think you read the rest
of the stuff I pointed you at either. You might have scanned them, but I
don't think you took the time to read them thoroughly.

Read these again:
http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_08.html
http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_09.html
http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_11.html
http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_12.html
http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_14.html
http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_15.html

Make sure to actually *read them* (and understand them), rather than just
scanning your eyes over them.

Also go over the example code in there, and make sure you correlate the
descriptive content with what they show in the code.

Then you'll know pretty much everything you need to know on the topic.
Cool!

--
Adam
Translate
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 ,
Aug 17, 2008 Aug 17, 2008
First, you transferred code that was supposed to run at the end of a request to one that is supposed to run at the end of a session.

Second, while not manadatory, it is a good idea to use the var keyword whenever setting a variable. This keeps it local and prevents the unintentional overwriting of variables in the calling template.

Third, the way I understand it, the this keyword applies to the entire cfc, not just the function. Also, it means that the variable value can be changed outside the function. Sometimes you want that, sometimes you don't. Changing the application name every time you start a session is probably something you don't want.
Translate
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 ,
Aug 17, 2008 Aug 17, 2008
Hydrowizard,
it is indeed a good idea to move to Application.cfc. In any case, you should be aware of the following corrections to your original code:

Application.cfm (with capital A)
OnRequestEnd.cfm (with capital O, R, E)
name = "mynew_app" (avoid space in application name)
avoid HTML tags in Application.cfm and OnRequestEnd.cfm

Having said that, your example might be translated into the following Application.cfc. There is a full example of Application.cfc in the livedocs. Have a look at it.



Translate
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
Participant ,
Aug 19, 2008 Aug 19, 2008
Thanks BKBK after Adam's advice to RTFM! I came up with this version which is almost like yours, mine wasn't working but I know now *why* I thought I did and it seems to be "working" now but I don't understand the concept please see below
Translate
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
Participant ,
Aug 19, 2008 Aug 19, 2008
Right two questions please
1. I just went through the code and I saw BKBK's commented out part saying "do you want to have time in london and login on each page" the answer is yes as I just want to replicate the OnRequestEnd.cfm that I have with the Application.cfm so this all is a bit strange. If I can't have html in the Application.cfc then do I just use a cfinclude instead and include the code on my former OnRequestEnd.cfm?
2. I had to include my index.cfm page on the OnRequestStart function to make it show but none of the links work. Do I have to cfinlcude all the .cfm pages now?!! Confused!! What do I do?!! Thanks
Translate
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 ,
Aug 19, 2008 Aug 19, 2008
> <cffunction name="onApplicationEnd" hint="The application ends when the
> application times out or the server shuts down">
> <cfscript>
> StructClear(session);
> </cfscript>
> </cffunction>

You should not be doing anything to the session scope in OnApplicationEnd.
For one thing... whose session scope do you think this is? On
ApplicationEnd is not triggered by your site visitors. In fact if
intrinsically *can't* be triggered by your users, because if anyone's on
the site, then the application timeout will not trip. So if this even
works (I suspect it won't), then it won't be clearing anything sensible.

Also, why this:
<cfscript>
StructClear(session);
</cfscript>

When you could do this:
<cfset StructClear(session)>

?

--
Adam
Translate
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 ,
Aug 19, 2008 Aug 19, 2008
Read Adam's references again. Pay particular attention to the onRequest function.
Translate
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 ,
Aug 19, 2008 Aug 19, 2008
> Read Adam's references again. Pay particular attention to the onRequest function.

You beat me to it.

That said, I dunno why the links don't work just because of this.
Hyperlinks are a HTML construct which have no bearing and are not impacted
by however the underlying CF code is being called.

Hydrowizard:
What you you mean by "don't work"?

--
Adam
Translate
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 ,
Aug 20, 2008 Aug 20, 2008
If I can't have html in the Application.cfc then do I just use a cfinclude instead and include the code on my former OnRequestEnd.cfm?

You could just remove tags like <body>, <html> etc.

I had to include my index.cfm page on the OnRequestStart
function to make it show but none of the links work. Do I have to
cfinlcude all the .cfm pages now?!! Confused!! What do I do?!!


1) Make sure Application.cfc and index.cfm are in the same directory.

2) Comment out the function onRequest. You apparently don't need it. Besides, it is presently incomplete.

If you decide to use the onRequest method, then it must explicitly call the target page, for example, by means of a cfinclude tag.

<cffunction name="onRequest">
<cfargument name="targetPage" type="String" required=true/>
<cfinclude template="#Arguments.targetPage#">
</cffunction>




Translate
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
Participant ,
Aug 21, 2008 Aug 21, 2008
Thanks for those replies, but not working I mean that the links although they show in the index.cfm they don't work ie they don't go anywhere. 1) Make sure Application.cfc and index.cfm are in the same directory- they are

2) Comment out the function onRequest. You apparently don't need it. Besides, it is presently incomplete- right I've got some work to do I'll get cracking and I hope to be able to work it out with all your excellent help. I also RTFM again for the onrequest function.

Thanks!!
Translate
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 ,
Aug 21, 2008 Aug 21, 2008
> they don't go anywhere.

"They don't go anywhere", or "they return a 404/file not found error"?

I presume you mean you get a 404.

What's the URL, and how does that differ from where the file actually is?
HTTP is fairly simple: one asks for a file at a particular location, and
the web server returns it. However one might run into problems if the
locations one suggests in the hyperlink doesn't actually point to where the
file is...

--
Adam
Translate
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
Participant ,
Aug 26, 2008 Aug 26, 2008
I am getting there thanks everyone, putting the cfincludes on the onrequeststart method seems to have sorted out all those linking issues, the onrequest method is commented out as well.

I scoped the local variables with var.

1.With regards to application.name or this.name the docs seem to say that it should be this.name but the docs are notoriously wrong in many parts I know: (Define the application and give it a name by setting the This.name variable in the Application.cfc initialization section, before the method definitions.)

2.On the onrequestend method I know I shouldn´t have html and body tags so should I cfinclude a template instead?

3.BKBK if I used the onrequest method then where and how do I set the #Arguments.targetPage# value?
<cffunction name="onRequest">
<cfargument name="targetPage" type="String" required=true/>
<cfinclude template="#Arguments.targetPage#">
</cffunction>


Here´s my Application.cfc which is now looking a lot better having understood all your points fully:
Translate
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
Guest
Aug 26, 2008 Aug 26, 2008
Her eis another GREAT link for you to take a peak at - Ben did a good job spelling this out for us......

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

Hope it helps some.
Chris
Translate
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 ,
Aug 26, 2008 Aug 26, 2008
3.BKBK if I used the onrequest method then where and how do I set the #Arguments.targetPage# value?

<cffunction name="onRequest">
<cfargument name="targetPage" type="String" required=true/>
<cfinclude template="#Arguments.targetPage#">
</cffunction>


Something like that, actually.

Translate
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
Participant ,
Aug 27, 2008 Aug 27, 2008
ah so yeah I asked the wrong question then!doh! -it was how do I pass the value to the argument and why would I want to anyway?! Thank you #arguments.targetpage#
Translate
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 ,
Aug 27, 2008 Aug 27, 2008
quote:

Originally posted by: Hydrowizard
ah so yeah I asked the wrong question then!doh! -it was how do I pass the value to the argument and why would I want to anyway?! Thank you #arguments.targetpage#


Interesting question. You would want to pass the argument or the page won't work. I've only written one application.cfc file and I didn't use an onRequest function. I did use onRequestStart and onRequestEnd though.

Looking at your original question, I don't see where you need an onRequest function either.
Translate
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 ,
Aug 27, 2008 Aug 27, 2008
Hydrowizard, you might wanna have a look at another thread on Application.cfc
Translate
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
Participant ,
Aug 27, 2008 Aug 27, 2008
Thanks BKBK that is an interesting thread. Last small question- I think I know the answer- but the code I have in the onRequestEnd method could (and should as I have html tags in it) be on an cfinclude instead? TIA

<cffunction name="onRequestEnd" hint="All pages and CFCs in the request have been processed: equivalent to the OnRequestEnd.cfm page">
<cfargument type="String" name = "targetTemplate" required=true/>
<!--- code to run when a request ends --->
<div id="footer">
<p><cfscript>
tzoneObj = createObject("java","java.util.TimeZone");
tzone = tzoneObj.getTimeZone("Europe/London");
date_format = createObject("java","java.text.DateFormat").getDateTimeInstance();
date_format.setTimeZone(tzone);
writeoutput("Time London: "&#date_format.format(now())#);
</cfscript>
<a href="login.cfm">login</a></p></div></div>
</body></html>
</cffunction>
Translate
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 ,
Aug 27, 2008 Aug 27, 2008
I prefer seeing the code up front, instead of a cfinclude. Again the html tags are unnecessary.

Translate
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
Participant ,
Aug 29, 2008 Aug 29, 2008
I don't understand I need closing tags otherwise my xhtml won't validate!!
Translate
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 ,
Aug 29, 2008 Aug 29, 2008
I need closing tags otherwise my xhtml won't validate

I understand. However, you may omit the tags in pairs, hence, <body> and </body>, <html> and </html> and so on.
Translate
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