In this hands on we will create an Application.cfc file, create some application wide variables, and implement some of the Application.cfc event handlers.
Functions Used: createTimeSpan, structKeyExists, createObject
- Create a new file called Application.cfc in /www/.
- Open up the /www/Application.cfc file in your code editor. Note: for this hands on we will be doing all our coding in script format.
-
Create a component declaration. Your code should look similar to this:
component{ }
- Create two variables in the this scope called name and datasource. Set both variables to 'learncfinaweek'.
-
Create another variable in the this scope called applicationTimeout and give it a value of:
CreateTimeSpan(10, 0, 0, 0);
</li> <li> Create another variable in the this scope called <span class="code">sessionManagement</span> and set it to true. </li> <li> Create another variable in the this scope called <span class="code">sessionTimeout</span> and give it a value of:
CreateTimeSpan(0, 0, 30, 0);
</li> <li> Your code should look similar to this:
component{ this.name='learncfinaweek'; this.datasource='learncfinaweek'; this.applicationTimeout = CreateTimeSpan(10, 0, 0, 0); this.sessionManagement = true; this.sessionTimeout = CreateTimeSpan(0, 0, 30, 0); }
</li> <li> Create a function called <span class="code">onApplicationStart</span> that accepts no arguments and returns true. </li> <li> Inside the <span class="code">onApplicationStart</span> function, create two variables set in the application scope. The first variable should be called <span class="code">myName</span> and should have the value of your name. The second variable should be called <span class="code">myPosition</span> and should have the value of 'A Developer'. </li> <li> Your code should look similar to this:
component{ this.name='learncfinaweek'; this.datasource='learncfinaweek'; this.applicationTimeout = CreateTimeSpan(10, 0, 0, 0); this.sessionManagement = true; this.sessionTimeout = CreateTimeSpan(0, 0, 30, 0); function onApplicationStart() { application.myName = 'Simon'; application.myPosition = 'A Developer'; return true; } }
</li> <li> Below the <span class="code">onApplicationStart</span> function,create a new function called <span class="code">onRequestStart</span> which accepts one string parameter called 'targetPage'. </li> <li> Inside the function, create an <span class="code">if</span> statement that calls the <span class="code">StructKeyExists</span> function. The <span class="code">structKeyExists</span> function should be passed the URL scope as its first parameter and the string 'reload' as its second parameter. </li> <li> Inside the <span class="code">if</span> statement, a call should be made to the <span class="code">onApplicationStart</span> function. </li> <li> Your code should look similar to this:
function onRequestStart(string targetPage){ if(structKeyExists(url,'reload')){ onApplicationStart(); } }
</li> <li> Open up the <span class="code">/www/index.cfm</span> file in your code editor. </li> <li> Remove the <span class="code"><cfset></span> tags at the top of the page. </li> <li> Locate the <span class="code">myName</span> variable output on or around line 15 and change it to <span class="code">application.myName</span>. </li> <li> Locate the <span class="code">myPosition</span> variable output on or around line 16 and change it to <span class="code">application.myPosition</span>. </li> <li> In your browser, navigate to the <span class="code">/www/index.cfm</span> page. The index page should display as normal. </li> <li> Go to the <span class="code">/www/application.cfc</span> file in your code editor and change the <span class="code">application.myPosition</span> variable to have the value of 'A Great Developer'. </li> <li> In your browser, refresh the <span class="code">index.cfm</span> page. Notice that nothing has changed. This is because the application has already started, so the <span class="code">onApplicationStart</span> method does not get called. </li> <li> In your browser, append <span class="code">?reload=1</span> to the <span class="code">index.cfm</span> in the location bar. Hit return to load the page. Notice that the position has now updated to the new value. </li> <li> Open up the <span class="code">/www/resume.cfm</span> file in your code editor. Locate both <span class="code"><cfquery></span> tags and remove the <span class="code">datasource</span> attributes. </li> <li> In a browser, navigate to the <span class="code">/www/resume.cfm</span> page. Notice that the page loads normally. Both queries are now using the application wide data source. </li> <li> In the <span class="code">onApplicationStart</span> function in the <span class="code">/www/Application.cfc</span> file, create a new application variable called 'utilities'. Set the value of <span class="code">application.utilities</span> to:
CreateObject('cfc.utilities');
</li> <li> Your code should look similar to:
function onApplicationStart() { application.myName = 'Simon'; application.myPosition = 'A Great Developer'; application.utilities = CreateObject('cfc.utilities'); return true; }
</li> <li> Open up the <span class="code">/www/about.cfm</span> file in your code editor and remove the <span class="code"><cfset></span> tag on or around line 4. </li> <li> Locate the <span class="code">utilities.convertStringToASCII</span> function call on or around line 58 and change it to <span class="code">application.utilities.convertStringToASCII</span>. </li> <li> In a browser, navigate to the <span class="code">/www/about.cfm</span> page, remembering to include <span class="code">?reload=1</span> in the URL as we have made an <span class="code">application.cfc</span> change. </li> <li> Notice that the page loads normally. The <span class="code">convertStringToASCII</span> function is now stored in the application scope and can be accessed from any page within the application. </li> </ol>
- Remove the datasource attribute from all query tags.
- Update the email address in the /www/contact.cfm file to use the convertStringToASCII function.