Adding and using configuration settings
From Achievo/ATK Wiki
|
ATK Howto: Adding and using configuration settings
|
Global Configuration
Adding a new global configuration setting can be very useful, and in ATK its very easy. In config.inc.php, just add a line which sets the variable, e.g.:
$config_foo = 'bar';
The variable name must start with $config.
To retrieve variables, either ones you've added or existing ones, use the atkconfig() function and supply the name of the variable you want.
$my_foo = atkconfig("foo"); // now $my_foo = 'bar';
This would return the variable $config_foo from the config file. Arrays can also be handled in this way. The atkconfig() call can be used from within your module and attribute classes.
It's also possible to use default values:
$my_foo = atkconfig("foo", "bar");
In this example, if $config_foo isn't defines, "bar" is returned as the value for this config.
Module Configuration
The global configuration file is very handy, but it can become bulky. It's also possible to have configuration files per module. This increases the reusability of modules, as each module can come with its own configuration file. Not only that, the module based configuration uses a more modern OO API using the atkConfig class.
If your module is named 'foo', you would create a module configuration file by creating a file called:
configs/foo.inc.php
The contents of foo.inc.php could look like this:
<?php $config["hello"] = "world";
To retrieve the value of this configuration value, you would do:
$value = atkConfig::get("foo", "hello");
or with defaults:
$value = atkConfig::get("foo", "hello", "world");
You can also use this mechanism to just group config variables into a separate file. The 'foo' in 'foo.inc.php' does not have to be a module. Say you have a lot of configuration values having to do with mail stuff, you could use a mail.inc.php and use atkConfig::get("mail", "smtpserver"); to retrieve values from it.