cfTrigger libraries > Application component

Intro

File location: ROOT/cftrigger/system/libraries/Application.cfc

This is essentially the Application.cfc file defined by Adobe Coldfusion with some prewritten code to handle the request when it comes and preload cfTrigger libraries when application starts. Because the Application.cfc file is executed before anything else, it is arguably the most important file. It validates the request when it arrives, loads the appropriate controller or returns any error (such as 404) that it encounters during the loading process.

Available functions

1. OnApplicationStart

OnApplicationStart is a predefined name for a method that is invokved when the application starts the very first time (or after the server restarts). Read more about OnApplicationStart on Adobe.

In cfTrigger, a number of important activities are carried inside this method:

  • It checks for what server the application is running on and load a number of variables accordingly. NOTE: if the server where the application is running on is not defined in the server list (the server list is defined inside the config.cfm file), it will throw an error and stop further processing. So you encounter the following error, double check that the server (either development or production) is included on the server list:

    INVALID SERVER. THE APPLICATION IS NOT ALLOWED TO RUN ON THIS SERVER
  • Preload a number of application scope variables. The following variables are automatically available for use anywhere in the application:

    Server settings:

    • application.name: if running on live server, this is the name of the application as defined inside the config.cfm file. If running on development server, its name will be the application name plus (dev)
    • application.serverType: contains the type of the current server as defined inside the config.cfm file. For example, it contains dev for development server and live for live servers.
    • application.serverName: contains the name of the current server as defined inside the config.cfm file prefixed with the server type mentioned above. For example, if the server name is defined as localhost inside the config file with the type of dev, the actual value of application.serverName is dev_localhost. The reason is someitmes the development and live applications could be sitting on the same server but different folders. Without the server type prefix, we could not differentiate them.
    • application.rootURL: contains the url of the current server as defined inside the config.cfm file
    • application.baseURL: similar to the application.rootURL but with the filename index.cfm
    • application.separator: the operating system separator ("/" or "\")

    Database settings:

    • application.dbname: the name of the database being used. It will be either name of the live or dev database depending on the server it's running on
    • application.dbuser: the username used to connect to the database being used. It will be either username of the live or dev connection depending on the server it's running on
    • application.dbpassword: the password used to connect to the database being used. It will be either password of the live or dev connection depending on the server it's running on

    Logical paths:

    • application.modelPath, application.viewPath, application.controllerPath, application.libraryPath, application.errorPath: logical path to the models folder, views folder, controllers folder, libraries folder and errors folder respectively

    Absolute paths / File paths:

    • application.modelFilePath, application.viewFilePath, application.controllerFilePath, application.libraryFilePath, application.errorFilePath: absolute file paths to the models folder, views folder, controllers folder, libraries folder and errors folder respectively
    • application.CFT_LibraryFilePath: the absolute file path to the cfTrigger libraries folder

    Package paths:

    • application.modelRoot: the path to models package using dot (.) style
    • application.controllerRoot: the path to controllers package using dot (.) style
    • application.libraryRoot: the path to libraries package using dot (.) style

    As a summary, for the current example, we have the following variables available on the localhost once the application starts:

    Variable Value
    Server settings
    application.name MySite (dev)
    application.serverType dev
    application.serverName dev_localhost
    application.rootURL http://localhost/mysite
    application.baseURL http://localhost/mysite/index.cfm
    application.separator \
    Database settings
    application.dbname mysite_dev
    application.dbuser [empty string]
    application.dbpassword [empty string]
    Logical paths
    application.modelPath /mysite/application/models
    application.viewPath /mysite/application/views
    application.controllerPath /mysite/application/controllers
    application.libraryPath /mysite/application/libraries
    application.errorPath /mysite/application/errors
    Absolute paths / File paths
    application.modelFilePath C:\ColdFusion8\wwwroot\mysite\application\models\
    application.viewFilePath C:\ColdFusion8\wwwroot\mysite\application\views\
    application.controllerFilePath C:\ColdFusion8\wwwroot\mysite\application\controllers\
    application.libraryFilePath C:\ColdFusion8\wwwroot\mysite\application\libraries\
    application.errorFilePath C:\ColdFusion8\wwwroot\mysite\application\errors\
    application.CFT_LibraryFilePath C:\ColdFusion8\wwwroot\cftrigger\system\libraries\
    Package paths:
    application.modelRoot mysite.application.models
    application.controllerRoot mysite.application.controllers
    application.libraryRoot mysite.application.libraries
  • Preload cfTrigger libraries. Most of the cfTrigger libraries are preloaded when the application starts and stored inside memory (application scope) to optimize the performance.

2. OnRequestStart

OnRequestStart is a predefined name for a method that is invokved everytime a request is received. Read more about OnRequestStart on Adobe.

In cfTrigger, a number of important activities are carried inside this method:

  • Pass in reset = 1 in the url to reset the application scope variables (call the OnApplicationStart method)
  • Pass in logout = 1 in the url to log user out or just to clear user session
  • Initialize the form and url variables: controller, view, Id and textId (see below)
  • Perform authentication on every request if application.enableUserAuthentication is turned on (set to true) inside the config.cfm file.
  • Validate the existence of controller and view and then load them based on user request
  • Load the view directly if directView is specified inside the config.cfm file and the view is matched with what's on that list
  • Throw 404 error if the controller is not found or it is found but the view is not found for that controller

request.currentPage: whenever a request starts, this variable contains the current path. For example: http://localhost/mysite/index.cfm/blog/view/12345?showComments=1. The url variable is also included.

Ask a Question on cfTrigger