element
Configuration
Overview
The configuration can be found in /app/configuration/configuration.php which contains a simple array.
Certain defaults are already set, but feel free to change them.
The main 'areas' in a basic setup are
QuickStart
To get up and running straight away, just fill in the db and domain sections
- domain:
- host = host name.
- root = empty if root, directory name if not.
- db: Settings that you would use for PDO connection
Access Settings
Custom Settings
There may be times you need to access the settings, either pre-configured or custom.
They can be accessed via static methods in the Config class
To add settings, we recommend you add a new nested array
Custom settings example
"customsettings" => [
"custom_one" => "foo",
"custom_two" => [
"deepdown" => "yeah"
]
]
Referencing the Config class
We use the Config class to easily read settings.
- If we are the the element\core namespace, we can simply call the static methods of the class.
- If we are in a controller, we can use $this->config.
- If none of the above, we must use the full namespace \element\core\Config
At the risk of labouring the point, here's a quick table to illustrate
Situation | Syntax |
---|---|
element\core namespace | Config::get(key, key) |
controller | $this->config::get(key, key) |
none of the above | \element\core\Config::get(key, key) |
In the following examples, we will use the plain 'Config' syntax
Get single value
Config::get(key,key);To get our 'custom_one' setting
$setting = Config::get("customsettings","custom_one");
// $setting has value of 'foo'
Get whole section
Config::getSection(key);To get our 'customsettings' section
$settings = Config::getSection('customsettings');
var_dump($settings);
/* --- Output ---
array (size=2)
'custom_one' => string 'foo' (length=3)
'custom_two' =>
array (size=1)
'deepdown' => string 'yeah' (length=4)
*/
Deeply nested setting
Config::Instance()->_config[key][key][…]Access our 'deepdown' setting
$setting = Config::Instance()->_config['customsettings']['custom_two']['deepdown'];
// $setting has value of 'yeah'
Domain
Provides information about the location of your site.
Example
"domain" =>[
"host" => "localhost/",
"root" => "element"
],
host
Describes your ... host name!
[subdomains].[second-level-domain].[top-level-domain]
ie: www.coooffffeee.com
root
The folder your files are in.
If this actually is the root folder, this should be an empty string.
DB
The db section contains the information you would use to create a PDO connection.
"db" => [
"dsn" => "[database]",
"host" => "[host]",
"user" => "[database-user]",
"pw" => "[password]"
]
Let's convert this to a single array for an example
$db = [
"dsn" => "bigdavesdatabase",
"host" => "localhost",
"user" => "bigdave",
"pw" => "bigdave-password"
];
Now we can form the connection and get results using PDO
$CONN = new PDO(
"mysql:host=" . $db['host'] .
";dbname=" . $db['dsn'],
$db['user'],
$db['pw']
);
$STH = $CONN->query('SELECT * FROM users');
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($row = $STH->fetch()) {
var_dump($row);
}
NOTE: In element, database access is hidden under layers of abstraction, with methods and models to assist, so there should be no need to directly instantiate PDO: this is simply for example
Defaults
This section contains the standard settings for the framework itself
"defaults" => [
"controller_suffix" => "Controller",
"action_suffix" => "Action",
"default_controller" => "Index",
"default_action" => "index"
]
controller_suffix
When you create a controller class:
- Class must extend Controller class
- File must be in /app/controllers/
- The file name and class 'suffix' must match what you define as your 'controller_suffix'
Standard example
// File name 'IndexController.php'
class IndexController extends Controller {
// methods
}
Custom controller_suffix of 'Class'
// File name 'IndexClass.php'
class IndexClass extends Controller {
// methods
}
More information on controllers and their methods can be found here
action_suffix
Within each controller, there must be methods in order to trigger an action.
The default suffix is 'Action'
Standard example
public function IndexAction(){
// make wonderful things happen here
}
Custom action_suffix of 'Method'
public function IndexMethod(){
// be truly splendid here
}
More information on controllers and their methods can be found here
default_controller
If no controller is supplied in the URL, this is the default controller the framework will serve up
URL structure http:// [yoursite.com] / [controller] / [method] / [parameter1] / [parameter2] ...
Example URL with controller:
http://yoursite.com/index
Example URL without controller:
http://yoursite.com
default_action
If no method is supplied in the URL, this is the default method the framework will serve up
URL structure http:// [yoursite.com] / [controller] / [method] / [parameter1] / [parameter2] ...
Example URL with method:
http://yoursite.com/index/index
Example URL without method:
http://yoursite.com/index
View
element uses Smarty for views and this section refers to settings
for that.
Documentation on Smarty can be found here
For the methods available in element for setting view variables and overriding views, see Controllers > Views.
We recommend that in production you set caching to a valid number of seconds
Default view settings
"view" => [
"templateDir" => '../app/views/',
"compileDir" => '../app/templates_c/',
"cacheDir" => '../app/cache/',
"caching" => 0 // change this value in production
]
Errors
The errors section currently has only two options, redirect and redirect_location
This is primarily for handling 404 errors at the moment.
The redirect_location will automatically prepend protocal, 'host' and 'root', so 404.html becomes https://localhost/element/404.html, and because of the way .htaccess works in public, the file is found.
At the time of writing, only 404 errors on controllers redirect, actions throw 'no action with that name' error.