The ColdFusion Application ‘Framework is a set of Cold Fusion templates that enables you to:
• Maintain state by setting variables that can be accessed from any template in your application framework.
• Provide custom error messages. Customized error handling is discussed in more detail in Chapter 18, “Implementing Error Control.
• Enhance the security of an application. Application security is discussed in more detail in Chapter 30, “Application Security.”
In this chapter, you will learn how to maintain variable states by setting up and using the features of the ColdFusion Application Framework.
Creating the ColdFusion Application Framework
Until this point, you have coded ColdFusion templates that enable you to query databases, create arrays and lists, validate form input, and pass variables between templates. Yet you have not been able to maintain state over a series of templates. Maintaining state enables you to access information set in previous templates without having to pass that information between templates. In past chapters, whenever you wanted to access variables set in a previous template, you had to ensure that they were passed along either through URL or form parameters, or by storing them in a database for later access. ColdFusion lets you also store this infonnation in variables that are available to any template within your application. The types of variables that are designed to work with the ColdFusion Application Framework are listed in Table 17.1. You will learn more about creating and managing these variables later in this chapter.
Creating the Application cdm Template
To use the ColdFusion Application Framework, you must first create a single application template in the root directory of your application. This template must be named Application. cfm . After the Application. cfm template has been created, it will be included at the beginning of every template processed within the application framework.
Creating the On Request End. cfm Template
From ColdFusion 4.0.1 you can also create a template that will be processed at the end of every page in your application framework. You can use this template, called OnRequest-End. cfm, to create a standard footer for all pages of your application or to execute a set of code after all the processing in the caller template has occurred. This is an optional template and is not required to make the application framework operational. This template must reside in the same directory as the template being processed. If you create this template, it cannot be of zero length, or ColdFusion will produce an error.
Understanding the Application Directory Structure
The application framework consists of all templates in the specified directory and any sub directories. It is processed as follows:
1. The user connects to a ColdFusion template.
2. Before the ‘template is processed, Cold Fusion checks the directory in which the template resides for an Appl t cat; on. cfm template.
3. If no App1; cat; on. cfm is found, ColdFusion looks to the next higher level directory until one is found.
4. If no Application. cfm is ever found, then processing of the template takes placeas usual, and it is not tied to any Cold Fusion application.
5. If Application. cfm is found, App1;cat; on. cfm is processed first, and then the template is processed.
6. After the template has been processed, ColdFusion searches the same directory for OnRequestEnd.cfm.
7. If no OnRequestEnd. cfm is found, nothing else happens (ColdFusion looks only in the current directory for OnRequestEnd. cfm).
8. If OnRequestEnd. cfm is found in the same directory, then it is processed and included at the end of the template.
The Application. cfm template is useful because in it you can create global settings that can then be accessed by all templates in your application directory structure. You can use OnRequestEnd. cfm to process a standard footer for every template in your application.
Look at the example in Figure 17.2. When the; index. cfm template is called, ColdFusion checks whether an Appl icat; on. cfm template is in the chapter1? directory.
Because one is not there, Cold Fusion then looks in its parent directory, symbex. Because an Application. cfm template is in the sybex directory, it is processed first, and then the; index. cfm template is processed.
In the example, when the index , cfm template is called, ColdFusion finds an Applicaton. cfm template in the directory. This Applicat; on. cfm template is processed instead of the one in the sybex parent directory.
Only one App1; cat; on. cfm template and one OnRequestEnd. cfm template is ever processed. Remember this when you arc including other files within your template. If you include the log; n. cfm template in the home. cfm template by using the CFINCLUDE tag, the only Application. cfm file that will be processed is the one in the sybex directory if you want the Application. cfm template in the directory to be processed, then you must explicitly include it by using the CFINCLUDE tag.