<<

Application.cfc

Hands On 16

By Simon Free (Bio)
gist

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.

Tags Used: <cfset>, <cfquery

Functions Used: createTimeSpan, structKeyExists, createObject

  1. Create a new file called Application.cfc in /www/.
  2. 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.
  3. Create a component declaration. Your code should look similar to this:
    component{
    }
    
  4. Create two variables in the this scope called name and datasource. Set both variables to 'learncfinaweek'.
  5. Create another variable in the this scope called applicationTimeout and give it a value of:
    CreateTimeSpan(10, 0, 0, 0);
    
  6. Create another variable in the this scope called sessionManagement and set it to true.
  7. Create another variable in the this scope called sessionTimeout and give it a value of:
    CreateTimeSpan(0, 0, 30, 0);
    
  8. 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);
    }
    
  9. Create a function called onApplicationStart that accepts no arguments and returns true.
  10. Inside the onApplicationStart function, create two variables set in the application scope. The first variable should be called myName and should have the value of your name. The second variable should be called myPosition and should have the value of 'A Developer'.
  11. 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;
        }
    }
    
  12. Below the onApplicationStart function,create a new function called onRequestStart which accepts one string parameter called 'targetPage'.
  13. Inside the function, create an if statement that calls the StructKeyExists function. The structKeyExists function should be passed the URL scope as its first parameter and the string 'reload' as its second parameter.
  14. Inside the if statement, a call should be made to the onApplicationStart function.
  15. Your code should look similar to this:
    function onRequestStart(string targetPage){
    	if(structKeyExists(url,'reload')){
    		onApplicationStart();
    	}
     }
    
  16. Open up the /www/index.cfm file in your code editor.
  17. Remove the <cfset> tags at the top of the page.
  18. Locate the myName variable output on or around line 15 and change it to application.myName.
  19. Locate the myPosition variable output on or around line 16 and change it to application.myPosition.
  20. In your browser, navigate to the /www/index.cfm page. The index page should display as normal.
  21. Go to the /www/application.cfc file in your code editor and change the application.myPosition variable to have the value of 'A Great Developer'.
  22. In your browser, refresh the index.cfm page. Notice that nothing has changed. This is because the application has already started, so the onApplicationStart method does not get called.
  23. In your browser, append ?reload=1 to the index.cfm in the location bar. Hit return to load the page. Notice that the position has now updated to the new value.
  24. Open up the /www/resume.cfm file in your code editor. Locate both <cfquery> tags and remove the datasource attributes.
  25. In a browser, navigate to the /www/resume.cfm page. Notice that the page loads normally. Both queries are now using the application wide data source.
  26. In the onApplicationStart function in the /www/Application.cfc file, create a new application variable called 'utilities'. Set the value of application.utilities to:
    CreateObject('cfc.utilities');
    
  27. Your code should look similar to:
    function onApplicationStart() {
    	application.myName = 'Simon';
    	application.myPosition = 'A Great Developer';
    	application.utilities = CreateObject('cfc.utilities');
    	return true;
    }
    
  28. Open up the /www/about.cfm file in your code editor and remove the <cfset> tag on or around line 4.
  29. Locate the utilities.convertStringToASCII function call on or around line 58 and change it to application.utilities.convertStringToASCII.
  30. In a browser, navigate to the /www/about.cfm page, remembering to include ?reload=1 in the URL as we have made an application.cfc change.
  31. Notice that the page loads normally. The convertStringToASCII function is now stored in the application scope and can be accessed from any page within the application.

Homework

  1. Remove the datasource attribute from all query tags.
  2. Update the email address in the /www/contact.cfm file to use the convertStringToASCII function.