Skip to content
dbald edited this page Sep 16, 2010 · 5 revisions

Configuration

Home > Configuration

The configuration for the framework is all done in the config folder.

General Configuration

The config.php file holds the main configuration for the framework. There is a lot that you can do in this file to get things prepared for your application such as starting sessions or setting up global variables.

The configuration options for the framework are:

<?php
/* This is the mode for the system to use and is used to determine is a branch, helper, or plugin should be loaded in.
    In some cases this is used to determine how to show errors. The options for this are: development, testing, production. */ 
Reg::set("System.mode", "development");

/* If set to true this will display the page load time, memory used, and queries executed on a page at the bottom of the page.
    Possible values are: true, false. */
Reg::set("System.displayPageLoadInfo", false);

/* Used to determine if the framework should use mod_rewrite or if it should use a querystring, e.g. index.php?url=.
    Possible values are: true, false. */
Reg::set("URI.useModRewrite", true);

/* This is the get variable to use when not using mod_rewrite. */
Reg::set("URI.prependIdentifier", "url");

/* Used to allow dashes and underscores in the URI. Possible values are: true, false. */
Reg::set("URI.useDashes", true);

/* Used to force dashes in the URI making underscores invalid. Possible values are: true, false. */
Reg::set("URI.forceDashes", true);

/* This is the map for the URI. Basically using this array you are naming the various elements in the URI.
    You can add as many items as you like to this array but the controller and the view keys are required.
    The values of each of the keys in the array are the default values for the URI elements if the URI doesn’t
    have enough items to fill up the array. In the example of what is set below the main controller is the default
    if no controller is defined and the index view is the default view that will be loaded if none is defined. */
Reg::set("URI.map", array(
	"controller"	 	=>  "main",
	"view" 			=>  "index",
	"action" 		=>  "",
	"id" 			=>  ""
));

/* This option allows any errors that happen in the page to be printed out on the page rather than just logging them.
    Possible values are: true, false. */
Reg::set("Error.viewErrors", true);

/* This options tells the system to log any errors that occur. Possible values are: true, false. */
Reg::set("Error.logErrors", true);

/* This is a general error message to use when an error occurs and the system mode is set to production. */
Reg::set("Error.generalErrorMessage", "An error occurred. Please contact the administrator.");

/* This option is used to set a url to load when an error with the error code, replaced by [error code], is thrown.
    Possible values are: a string, an array setup the same as the URI.map. */
Reg::set("Error.[error code]", "/error");

/* The framework counts how many queries are executed in the models by default but this option when set to
    true allows you to also store the whole query and it’s values so that you can use it to debug.
    Possible values are: true, false. */
Reg::set("Database.storeQueries", false);

/* The models in the framework use something called iteration. With iteration models will store all the results
    for a find or a get in one class. This is the default for the framework but that can be changed by setting this
    option to true. If this option is true then all finds and gets on the models will return an array filled with objects
    that only have one row of data populated in it. Possible values are: true, false. */
Reg::set("Database.autoExtract", false);

/* This is the database host for models to use. */
Reg::set("Database.host", "localhost");

/* This is the database username for models to use. */
Reg::set("Database.username", "root");

/* This is the database user’s password for models to use. */
Reg::set("Database.password", "password");

/* This is the database for the models to use. */
Reg::set("Database.database", "data");

/* This is the driver for the models to use to interact with the database. */
Reg::set("Database.driver", "MySQL");
?>

Automatically Setup Configuration Variables

There are variables in the framework that are setup automatically that are available for use also. These can be overridden but it is better to just use them in a get context as the framework relies on these variables to run.

<?php
/* This is the version of the framework. */
Reg::get("System.version");

/* This is how the system identifies the root structure. This is used when trying to load something such as a
    view and you want to specify that you want to load from the root and not a branch. This can be used in the
    branch argument of any method that supports specifying a branch. */
Reg::get("System.rootIdentifier", );

/* This is the physical path to the framework. */
Reg::get("Path.physical");

/* This is the URL based path to the root of the framework. This is typically used in links such as href's as the
    value of this variable is effected by your use of mod_rewrite. */
Reg::get("Path.site");

/* This is the URL based path to the root of the framework. This is typically used for files such as Javascript,
    CSS, and images as this variable's value is not effected by your use of mod_rewrite. */
Reg::get("Path.root");

/* This is the URL based path to the public folder. */
Reg::get("Path.skin");

/* This is the full current URI as shown in the address bar of your browser. */
Reg::get("Path.current");

/* This is the physical path on the server to the branch's root directory. */
Reg::get("Path.branchPhysical");

/* This is the current branch's equivalent to Path.site, so it gives you to URL based path to the branch root.
    This is typically used in links such as href's as the value of this variable is effected by your use of mod_rewrite. */
Reg::get("Path.branch");

/* This is the current branch's equivalent to Path.root, so it gives you the URL based path to the branch root.
    This is typically used for files such as Javascript, CSS, and images as this variable's value is not effected
    by your use of mod_rewrite. */
Reg::get("Path.branchRoot");

/* This is the URL based path to the current branch's public folder. */
Reg::get("Path.branchSkin");
?>

The framework also sets up path variables for each of the elements defined in the URI.map. These are mainly used for links so that you can navigate around your project using the same URI.map format that the framework is already using as shown in the example below.

<?php
/* This is an example list of some of the path variables defined from URI.map. */
Reg::get("Path.controller");
Reg::get("Path.view");
Reg::get("Path.action");
Reg::get("Path.id");
?>

The framework also automatically sets up Param variables for each of the elements defined in the URI.map and fills each variable with the appropriate value based on the current URI as shown below.

<?php
/* An example current URI. */
/products-services/light-bulbs

/* An example of the Param variables and their values based on the above URI. */
Reg::get("Param.controller");	// Outputs: products-services
Reg::get("Param.view");		// Outputs: light-bulbs
?>

Using the Param variables you are able to know exactly what the URI.map elements current values are and change what is happening in your code based on that.

Autoloading

There is some ability to add your own custom autoloaders to the framework to extent the abilities of the framework.

<?php
/* Setting this up in the config.php file will let you load in a custom set of classes using PHP 5.3 namespacing
    natively into the framework. */
Autoloader::registerNamespace("Namespace", Reg::get("Path.physical") . "/vendors/namespace");

/* Setting this up in the config.php file will let you load in a custom set of classes using the PEAR loading
    format natively into the framework. */
Autoloader::registerPrefixes("Prefix", Reg::get("Path.physical") . "/vendors/classes");

/* Setting this up in the config.php file will let you load in a custom set of classes that follow one of 4 formats.

The supported formats are:
Test_Class translates to test.class.php
Test_Class translated to test_class.php
Test_Class translates to testclass.php
Test_Class translates to TestClass.php
	*/
Autoloader::registerDirectory("Directory Identifier", Reg::get("Path.physical") . "/vendors/customclasses");

/* Setting this up in the config.php file will let you load in a specific file automatically when the framework loads. */
Autoloader::registerFile("File Identifier", Reg::get("Path.physical") . "/vendors/customfiles/index.php");
?>

Registering Errors

Evergreen has the ability to have errors that are pre-registered for ease of use so that when an error is thrown you can just use the key that the error was registered with all the thrown error will have all the properties of the error that was defined previously. Another benefit of this is being able to easily change the language of the errors that the framework uses without having to dig through the lib files. All the pre-registered errors are defined in the errors.php file.

To register an error you would call:

<?php
/* The first parameter is the key to register the error under and the second are the error’s attributes as an array. */
Config::registerError('SOME_KEY', array('message'=> 'The error message', 'code'=> 'GEN'));
?>

There are quite a few options that can be used to define an error.

The available options are:

<?php
array(
	/* The error message */
	'message' => 'The error message for %(className)s',
	
	/* The custom arguments to use in the error message so that you can customize your error message when
            it is triggered. The elements in this array are merged into the error message using sprintf with a modified
            ability to load in based on key as shown in the message example above. */
	'messageArgs' => array('className' => 'ProductsSolutions_Controller'),
	
	/* The code that the error is throwing */
	'code' => 404,
	
	/* The url to load when the error is thrown. This will do a header location redirect if the url is outside the
            framework. If the url is inside the framework the framework will attempt to load this url without
            redirecting the browser. */
	'url' => '/error'
);
?>

Reg Variables

The Reg class is used extensively in the configuration to hold variables. Reg is short for Registry and therefor the Reg class is the framework’s registry. It is intended to hold variables globally using a namespace to group the types of variables. The class is used by the framework to hold necessary settings and information but is also intended to be used freely by the developer to store any variables that he chooses. The developer might want to store persistent ftp information in these variables and he can do so in the config class, or anywhere he chooses, using syntax similar to the example below.

It is important to note that there is no limitation on what type of content can be stored in a Reg variable.

<?php
/* Example of storing custom info in the Reg class */
Reg::set('FTP.host', 'ftp.host.com');
Reg::set('FTP.username', 'username');
Reg::set('FTP.password', 'password');
Reg::set('FTP.port', '21');
?>

To retrieve the stored information you can use the Reg class’ get method as shown below.

<?php
/* Example of getting custom stored info from the Reg class */
Reg::get('FTP.host');
?>

The variable namespacing used by the Reg class can be used to any depth and as a result of the namespacing you can also get all your data in a group as shown in the example bellow.

<?php
/* Example of getting a group of custom stored info from the Reg class */
Reg::get('FTP');

/* The above would return */
array(
	'host' => 'ftp.host.com',
	'username' => 'username',
	'password' => 'password',
	'port' => '21'
)
?>

The Reg class has a few different methods besides the get and set methods to help you to interact with it’s variables as shown in the examples below.

<?php
/* Will return true if the variable is set and false if not. */
Reg::has('FTP.username');

/* Will return true if the variable exists and has a value and false if not. */
Reg::hasVal('FTP.username');

/* Will delete a variable. */
Reg::del('FTP.username');
?>

Prepending URI Items

When you define the URI map in the URI.map Reg variable the controller always has to be the first item in that array followed by the view so that the framework will be able to fill the array correctly. Branches act as a higher level folder or part of the framework and their position is not defined in the map as Evergreen can automatically detect them and fix the URI. Controllers need to be the first item in the URI because only a controller and a branch are able to be verified as actually existing in the framework. However there are times where you might want to have URI items appear before the controllers. There is a way to do this in the framework by defining an item in the URI.map that has a validator as shown in the example bellow.

<?php
/* Simple example of a prepended URI item */
Reg::set("URI.map", array(
	"lang"		=> array("en", create_function('$element', 'return preg_match("/(en|es)/i", $element);')),
	"controller"		=> "main",
	"view"		=> "index",
	"action"		=> null,
	"id"			=> null
));
?>

In the example above we are defining “lang” and a prepended URI item that has a default value of “en” if no value is matched, as a URI item at this level has to have a default. The URI item also has a simple function defined that is expecting one argument, the value of the URI item to test, and the function is returning true or false based on if the URI item being tested is matches “en” or “es”.

Instead of using create_function you can also use a Helper, Plugin, or a Model method as a validator for your URI item as shown in the example below.

<?php
Reg::set("URI.map", array(
	"lang"		=> array("en", array('Validation_Helper', 'checkLanguage')),
	"controller"		=> "main",
	"view"		=> "index",
	"action"		=> null,
	"id"			=> null
));
?>

Clone this wiki locally