Application Configurations

Application configurations are used to specify the global behavior of an application. They include specification of path aliases, namespace usages, module and service configurations, and parameters.

Configuration for an application is stored in an XML file named application.xml, which should be located under the application base path. Its format is shown in the following. Complete specification of application configurations can be found in the DTD and XSD files.

<application PropertyName="PropertyValue" ...>
  <paths>
    <alias id="AliasID" path="AliasPath" />
    <using namespace="Namespace" />
  </paths>
  <modules>
    <module id="ModuleID" class="ModuleClass"  PropertyName="PropertyValue" ... />
  </modules>
  <parameters>
    <parameter id="ParameterID" class="ParameterClass" PropertyName="PropertyValue" ... />
  </parameters>
  <include file="path.to.extconfig" when="PHP expression" />
  <services>
    <service id="ServiceID" class="ServiceClass" PropertyName="PropertyValue" ... />
  </services>
</application>
  • The outermost element <application> corresponds to the TApplication instance. The PropertyName="PropertyValue" pairs specify the initial values for the properties of TApplication.
  • The <paths> element contains the definition of path aliases and the PHP inclusion paths for the application. Each path alias is specified via an <alias> whose path attribute takes an absolute path or a path relative to the directory containing the application configuration file. The <using> element specifies a particular path (in terms of namespace) to be appended to the PHP include paths when the application runs. PRADO defines two default aliases: System and Application. The former refers to the PRADO framework root directory, and the latter refers to the directory containing the application configuration file.
  • The <modules> element contains the configurations for a list of modules. Each module is specified by a <module> element. Each module is uniquely identified by the id attribute and is of type class. The PropertyName="PropertyValue" pairs specify the initial values for the properties of the module.
  • The <parameters> element contains a list of application-level parameters that are accessible from anywhere in the application. You may specify component-typed parameters like specifying modules, or you may specify string-typed parameters which take a simpler format as follows,
    <parameter id="ParameterID" value="ParameterValue" />
    
    Note, if the value attribute is not specified, the whole parameter XML node (of type TXmlElement) will be returned as the parameter value. In addition, the System.Util.TParameterModule module provides a way to load parameters from an external XML file. See more details in its API documentation.
  • The <include> element allows one to include external configuration files. It has been introduced since v3.1.0. The file attribute specifies the external configuration file in namespace format. The extension name of the file must be .xml. The when attribute contains a PHP expression and is optional (defaults to true). Only when the expression evaluates true, will the external configuration file be included. The context of the expression is the application, i.e., $this in the expression would refer to the application object.
  • The <services> element is similar to the <modules> element. It mainly specifies the services provided by the application. Within a <service> element, one can have any of the above elements. They will be effective only when the corresponding service is being requested.

An external configuration file has the same format as described above. Although the name of the root element does not matter, it is recommended to be <configuration>. External configurations will append to the main configuration. For example, if a path alias is specified in an external configuration, it will become available in addition to those aliases specified in the main configuration.

By default without explicit configuration, a PRADO application will load a few core modules, such as THttpRequest, THttpResponse, etc. It will also provide the TPageService as a default service. Configuration and usage of these modules and services are covered in individual sections of this tutorial. Note, if your application takes default settings for these modules and service, you do not need to provide an application configuration. However, if these modules or services are not sufficient, or you want to change their behavior by configuring their property values, you will need an application configuration.

Available from Prado versions 3.2.2 onwards.

By default PRADO instanciates all modules defined in the application configuration at the beginning of the application lifecycle. This can hit the application performance if you have a lot of modules defined but not used at every request. Since version 3.2.2 you can set the lazy property on modules defined in the application configuration to enable the lazy loading of that module.

<modules>
    <module id="ModuleID" class="ModuleClass" lazy="true" PropertyName="PropertyValue" ... />
  </modules>
A module with the lazy property set won't be instanciated until the first time it gets actually used by the application:
// requesting the lazy module to the application will instanciate it
  Prado::getApplication()->getModule('ModuleID');

Available from Prado versions 3.2 onwards.

Since version 3.2 the application configuration can be stored in PHP array format in a file named application.php. The format of the configuration file is exactly the same of its XML counterpart, but following the PHP syntax.

<?php
return array(
  'application' => array(
    'PropertyName' => 'PropertyValue'
  ),
  'modules' => array(
    'ModuleID' => array(
      'class' => 'ModuleClass',
      'properties' => array(
        'PropertyName' => 'PropertyValue'
      ),
    ),
  ),
  'services' => array(
    'ServiceID' => array(
      'class' => 'ServiceClass',
      'properties' => array(
        'PropertyName' => 'PropertyValue'
      ),
    ),
  ),
);
The use of a PHP application configuration must be defined in the TApplication constructor, tipically located in the index.php entry script:
$application = new \Prado\TApplication('protected', false, TApplication::CONFIG_TYPE_PHP);
$application->run();