# Mapping Out an Application Framework

An important step in designing a ColdFusion application is mapping out its directory structure.

Before you start building the application, establish a root directory for the application. Application pages may be stored in subdirectories of the root directory.

When any ColdFusion application page is requested, ColdFusion searches up the page's directory tree for an `Application.cfm` file. When it is found, the `Application.cfm` code is logically included at the beginning of that page.

If it is not found, ColdFusion searches up the directory tree until it finds an `Application.cfm` file. If more than one `Application.cfm` file lives in the current directory tree, ColdFusion uses the first one it finds.

Just as the `Application.cfm` file is executed before each application page it governs, you can specify a file named `OnRequestEnd.cfm`, which is executed after each application page in the same application.

ColdFusion Server looks for the `OnRequestEnd.cfm` file in the same directory as the `Application.cfm` file of the current application page. The `OnRequestEnd.cfm` file will never be executed if it resides in another directory.

The `OnRequestEnd.cfm` file will not be executed if there is an error or an exception in the called page, or if the called page executes the CFABORT or CFEXIT tag.

Just as the `Application.cfm` file must be spelled with a capital A, you must spell the `OnRequestEnd.cfm` file with capital O, R, and E.

Defining a root directory for an application has a number of advantages:

- Development: The application is easier to develop and maintain because the application page files are well organized.
- Portability: The application can be more easily moved to another server or another part of a server without having to change any code in the application page files.
- Application-level Settings: Application pages that fall under the same root directory can share application-level settings and functions.
- Security: Application pages that fall under the same directory can share Web server security settings.

You can use a single `Application.cfm` file for your application, or use different `Application.cfm` files that govern individual sections of the application.

###

The directory trees below illustrate two approaches to implementing the Application Framework.

- In the first example, a company named Web Wonders, Inc. uses a single `Application.cfm` file installed in their application root directory to process all application page requests.
- The illustration on the right shows how Bandwidth Associates uses the settings in individual `Application.cfm` files to specify processing for ColdFusion applications at the departmental level. Only the Products application pages are processed using the settings in the root `Application.cfm` file. The Consulting, Marketing, and Sales directories each has its own `Application.cfm` file.

## Behavior with CFINCLUDE

Only one `Application.cfm` file is ever processed for each ColdFusion application page. The presence of an `Application.cfm` file is an implicit CFINCLUDE. If it is present in the directory tree, there is no way not to include it. For this reason, it is the ideal location to set application-level variables.

When the requested application page has a CFINCLUDE tag pointing to an additional application page, ColdFusion does not initiate another search up the directory tree based on the included application page. This is an important behavior to understand. Upon opening a requested application page, ColdFusion searches for the `Application.cfm` file only once.