Defining Custom Configuration Directives

Movable Type provides many ways for developers to collect configuration parameters and values. One such way is through a user interface from with the Preferences area in the application. This technique however is not always the most expedient or necessarily the most appropriate means to collect configuration data.

Another common mechanism for applications to be configured is through a configuration file. In fact this is exactly how Movable Type is configured primarily. Plugins are allowed to utilize this configuration file, called mt-config.cgi, as well for their own specific configuration parameters.

Each configuration directive needs to be registered with the system. This will assist Movable Type in detecting errors in the configuration file during initialization. To register a configuration directive, consult the following code sample:


sub init_registry {
    my $plugin = shift;
    $plugin->registry({
        config_settings => {
            'MyConfig' => {
                default => '/var/log/mt_error.log',
                path => 1
            },
        },
    });
}

Registering File Paths

There is an additional option path that can be used when registering a configuration parameter. This option should be used whenever the configuration directive stores a reference to a path on the filesystem (not a url, but a path). This enables Movable Type to convert all relative paths found in a config file to an absolute path automatically.

Your Configuration File

Once you have defined your configuration properties, you can place them in your Movable Type configuration file called mt-config.cgi and located in your MT_HOME directory (where mt.cgi is located). Here is a sample mt-config.cgi file based upon the code sample above:


## Movable Type Configuration File
##
#======== REQUIRED SETTINGS ==========
CGIPath        /var/www/cgi-bin/mt/
StaticWebPath  /mt-static/

#======== DATABASE SETTINGS ==========
ObjectDriver DBI::mysql
Database mtdb
DBUser Melody
DBPassword Nelson
DBHost localhost

#======== MAIL =======================
MailTransfer sendmail
SendMailPath /usr/sbin/sendmail

#======== MY SETTINGS================
MyConfig /usr/local/mt/logs/error_log

Note: While the core registry permits the declaration of configuration directives of type "SCALAR," "ARRAY" and "HASH" plugins are only permitted to register parameters to type scalar or in layman's terms: strings.

Access Configuration Properties

Finally, to access the values stored in your configuration file, you need to utilize the config method of Movable Type's core MT module, like so:


use MT;
sub some_method {
    my ($foo) = @_;
    my $log = MT->config('CustomErrorLog');
    # do stuff
}

When is it best to use a configuration file?

Given that there are multiple ways to collect configuration data, when is it best to use a configuration file instead of a user interface? This is actually a very difficult question to answer definitively. Consider the following:

  • Will the configuration property need to be modified frequently? If so, you may consider implementing a UI for shear convenience.
  • Will setting the configuration property need to be a highly privileged right on the system? If so, then by placing it is a config file you ensure that only users with direct access to the file system will have permission to edit it.
  • How quickly do I need to introduce the feature that requires this config property? If the answer is soon, then there is no quicker way to provide configurable features then a config file.
This page was last updated on 2007-07-23, 10:23.  

Leave a note