Question

I'm trying to setup my first application.cfc file.

I tried to invoke variables on an "event-basis" like so:

<cffunction name="onApplicationStart" returnType="boolean" output="false">
    <cfparam name="Application.RootDir" default="/">
    <cfreturn true>
</cffunction>

<cffunction name="onSessionStart" returnType="boolean" output="false">
    <cfparam name="Session.activeSession" default="Yes">
    <cfparam name="Session.activeLogin" default="No">
    <cfreturn true>
</cffunction>

I thought this would work nicely, but turns out, it does not, because I need to declare everything before, like so:

 <cfparam name="Application.RootDir" default="">
 <cfparam name="Session.activeSession" default="">
 <cfparam name="Session.activeLogin" default="">

My Question:
Why should I use CF-events for declaring variables. If I need to declare outside of an event anyway, I could save myself a line of code and declare&assign the inital value? Am I doing it wrong or are events only for assinging not for declaring?

Thanks for input!

Was it helpful?

Solution

What do you mean by "before" ("declare everything before")? Before what? The onApplicationStart() handler is the first thing run when an application starts, and the onSessionStart() handler is the first thing run when a session starts. So in the context of applications and sessions being established, there is no "before". I think you need to show us more code: eg some code that demonstrates them "not working".

That said, you don't show us the pseudo-constructor section of your Application.cfc, so it's impossible to tell what problems you have there, if any. But you need to enable session management before sessions will work, and you possible need to name your app before either application or session variables will stick, too (although they might just work with the nameless application too? Even if they do, it's best to name your app).

Lastly - and this will have no bearing on whether your variables are set or not, but you should be using <cfset> when setting variables, not <cfparam>. Both have the same effect here, but the former is the correct tool for the job.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top