Adding and using configuration settings: Difference between revisions
expanding a bit on the example |
No edit summary |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
{{howto|Easy|[[Lorna Mitchell]] | {{howto|Easy|[[Lorna Mitchell]]}} | ||
Adding a new 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.: | = 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.: | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
$config_foo = 'bar'; | $config_foo = 'bar'; | ||
| Line 13: | Line 15: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
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. | 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: | |||
<syntaxhighlight lang="php"> | |||
$my_foo = atkconfig("foo", "bar"); | |||
</syntaxhighlight> | |||
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: | |||
<syntaxhighlight lang="php"> | |||
<?php | |||
$config["hello"] = "world"; | |||
</syntaxhighlight> | |||
To retrieve the value of this configuration value, you would do: | |||
<syntaxhighlight lang="php"> | |||
$value = atkConfig::get("foo", "hello"); | |||
</syntaxhighlight> | |||
or with defaults: | |||
<syntaxhighlight lang="php"> | |||
$value = atkConfig::get("foo", "hello", "world"); | |||
</syntaxhighlight> | |||
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. | |||
Latest revision as of 09:56, 11 March 2011
|
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.