Configuration
- Environment
/.env
- Config files, -places and reading order
- Module's composer.json
- Access
MVC_*
config values - Access module config values
- Appendix
Environment /.env
MVC_ENV
In the root folder of your myMVC copy you will find an .env
file. There the variable MVC_ENV
declares what Environment is valid for myMVC.
You can name the value as you like - But common values are: develop
, test
, production
or live
.
# Environment
MVC_ENV=production
Custom .env variables
You are free to set up any key=value
declarations you want in that /.env
file.
Any key=value
pair found in the file will be set by putenv()
at start and you can access those variables via php's getenv()
command.
Example
# custom
foo=bar
Check
// string(3) "bar"
var_dump(
getenv('foo')
);
Config files, -places and reading order
The following locations for configurations exist and are read in succession in the appropriate order.
This also means that a later loaded configuration beats (overrides) an earlier loaded one.
- ⬆ myMVC config folder
/config/
- Any
*.php
file in this directory will be included; Reading order isa-z
- Coverage: globally - Configs placed here are valid to all modules and to all environments
- By default, the following files are located here
_mvc.php
: This file contains basic settings that are necessary for operation. You should not edit this file. If you want to change settings, override values in your own module config. myMVC config variable names always begin withMVC_
.- See content of file
/config/_mvc.php
- ⬆ Module's config folder (overrides 1.)
/modules/{moduleName}/etc/config/
- Any
*.php
file in this directory will be included; Reading order isa-z
- Coverage: module globally - Configs placed here are valid to all environments of your module
- By default, the following files are located here
_mvc.php
: further MVC configs. myMVC config variable names always begin withMVC_
.- See Example
/modules/Foo/etc/config/_mvc.php
- ⬆ Module's environment config file (overrides 2.)
- Schema:
/modules/{moduleName}/etc/config/{moduleName}/config/{environment}.php
- If your module is named
Foo
and you have setMVC_ENV
to'develop'
, your environment config file has to bemodules/Foo/etc/config/Foo/config/develop.php
. Make sure it exists.
- If your module is named
- Coverage: module environment specific - The concrete environment config file is loaded appropiate to your environment of your module you have set in
MVC_ENV
- See Example
/modules/Foo/etc/config/Foo/config/develop.php
- Schema:
⚠ If you create a module by using emvicy.php (see: Creating a primary Module), the corresponding "Module's environment config file" will be generated automatically.
But if you change the value of the MVC_ENV
variable of /.env
config file afterwards, make sure the corresponding "Module's environment config file" does exist in your module.
Example: If your Module is named Foo
, and you set MVC_ENV=production
, then the config file /modules/Foo/etc/config/Foo/config/production.php
has to exist.
Module's composer.json
Extend your module with third libraries using composer. You don't need to have composer installed locally, because myMVC has a composer.phar on board which is used to install and update libraries.
Write your requirements into the composer.json file which you find here:
composer.json
/modules/{moduleName}/etc/config/{moduleName}/composer.json
The next time you run your myMVC Application, myMVC will automatically perform a composer install
.
Alternatively you can of course also perform an installation by hand.
Installed vendor folder
/modules/{moduleName}/etc/config/{moduleName}/vendor
myMVC will automatically integrate all /vendor/autoload.php
autoloaders from any myMVC module.
keep vendor libraries updated
simply run emvicy.php:
php emvicy.php update
Access MVC_*
config values
Access MVC configurations via the \MVC\Config
Class. For each variable there is an identical getter.
Syntax
$mVar = \MVC\Config::get_{MVC_VARIABLE}();
Example
$sMvcBasePath = \MVC\Config::get_MVC_BASE_PATH();
Alternativley you can access all MVC Configs via \MVC\Registry
.
Alternative
$sMvcBasePath = \MVC\Registry::get('MVC_BASE_PATH');
Access module config values
Access module configuration array via the \MVC\Config
Class.
Example
$aModule = \MVC\Config::MODULE();
Alternativley you can access the module configuration array via \MVC\Registry
.
Alternative
$aModule = \MVC\Registry::get('MODULE');
Appendix
<?php
/**
* @package myMVC
* @copyright ueffing.net
* @author Guido K.B.W. Üffing <info@ueffing.net>
* @license GNU GENERAL PUBLIC LICENSE Version 3. See application/doc/COPYING
*/
MVC_RUNTIME_SETTINGS: {
// show error messages
error_reporting(E_ALL);
// enable exit on "kill" command and CLI break (CTRL-C)
// This command needs the pcntl extension to run.
// do not provide if php's builtin webserver is running (using e.g. php myMVC.phar)
if (true === isset($_SERVER['HTTP_HOST']) && '127.0.0.1:1969' !== $_SERVER['HTTP_HOST'])
{
(function_exists('pcntl_async_signals')) ? pcntl_async_signals(true) : false;
(function_exists('pcntl_signal')) ? pcntl_signal(SIGTERM, function(){exit();}) : false;
(function_exists('pcntl_signal')) ? pcntl_signal(SIGINT, function(){exit();}) : false;
}
/*
* @see http://www.php.net/manual/en/timezones.php
* @see http://stackoverflow.com/a/5559239/2487859
* to get array of available timezones see result of timezone_identifiers_list()
* try to get timezone (ubuntu), or set to UTC
*/
date_default_timezone_set(((file_exists('/etc/timezone')) ? trim(file_get_contents('/etc/timezone')) : 'UTC'));
setlocale(LC_ALL, 'C');
// show InfoTool bar
$aConfig['MVC_INFOTOOL_ENABLE'] = true;
// Log autoloader actions
$aConfig['MVC_LOG_AUTOLOADER'] = true;
}
MVC_BIN: {
$aConfig['MVC_BIN_PHP_BINARY'] = PHP_BINARY;
$aConfig['MVC_BIN_PS'] = whereis('ps'); # ps - report a snapshot of the current processes.
$aConfig['MVC_BIN_SED'] = whereis('sed'); # sed - stream editor for filtering and transforming text
$aConfig['MVC_BIN_MOVE'] = whereis('mv'); # mv - move (rename) files
$aConfig['MVC_BIN_GREP'] = whereis('grep'); # grep, egrep, fgrep, rgrep - print lines that match patterns
$aConfig['MVC_BIN_FIND'] = whereis('find'); # find - search for files in a directory hierarchy
$aConfig['MVC_BIN_REMOVE'] = whereis('rm'); # rm - remove files or directories
$aConfig['MVC_BIN_XARGS'] = whereis('xargs'); # xargs - build and execute command lines from standard input
}
MVC_APPLICATION_SETTINGS: {
/**
* keys for "query" notation in \MVC\Route routings
* e.g.: 'module=Foo&c=index&m=index'
*/
$aConfig['MVC_ROUTE_QUERY_PARAM_MODULE'] = 'module';
$aConfig['MVC_ROUTE_QUERY_PARAM_C'] = 'c';
$aConfig['MVC_ROUTE_QUERY_PARAM_M'] = 'm';
/**
* MVC fallback routing
* this routing will be used if none is specified for routing
* Note: Possibility of a direct call (http|cli) of this route is disabled
*/
$aConfig['MVC_ROUTING_FALLBACK'] =
$aConfig['MVC_ROUTE_QUERY_PARAM_MODULE'] . '=standard&'
. $aConfig['MVC_ROUTE_QUERY_PARAM_C'] . '=index&'
. $aConfig['MVC_ROUTE_QUERY_PARAM_M'] . '=fallback';
/**
* Name of method to be executed in the Target Controller Class
* before session and other main functionalities.
* It will be called in /application/library/MVC/Application.php:
* Controller::runTargetClassPreconstruct();
*
* This method is also declared via interface MVC_Interface_Controller.
* Due to this, you should not edit this name, otherwise you have to
* rename the method in that interface class, too.
*
* default:
* $aConfig['MVC_METHODNAME_PRECONSTRUCT'] = '__preconstruct';
*/
$aConfig['MVC_METHODNAME_PRECONSTRUCT'] = '__preconstruct';
/**
* Paths etc.
*/
$aConfig['MVC_WEB_ROOT'] = dirname($_SERVER['PHP_SELF']);
$aConfig['MVC_BASE_PATH'] = realpath(__DIR__ . '/../');
$aConfig['MVC_APPLICATION_PATH'] = $aConfig['MVC_BASE_PATH'] . '/application';
$aConfig['MVC_PUBLIC_PATH'] = $aConfig['MVC_BASE_PATH'] . '/public';
$aConfig['MVC_APPLICATION_CONFIG_DIR'] = $aConfig['MVC_APPLICATION_PATH'] . '/config';
$aConfig['MVC_VIEW_TEMPLATES'] = $aConfig['MVC_BASE_PATH'] . '/modules/Default/templates';
$aConfig['MVC_LIBRARY'] = $aConfig['MVC_APPLICATION_PATH'] . '/library';
$aConfig['MVC_MODULES_DIR'] = $aConfig['MVC_BASE_PATH'] . '/modules';
// Main myMVC config folder
$aConfig['MVC_CONFIG_DIR'] = $aConfig['MVC_BASE_PATH'] . '/config';
/**
* Log
*/
$aConfig['MVC_LOG_FILE_FOLDER'] = $aConfig['MVC_APPLICATION_PATH'] . '/log/';
$aConfig['MVC_LOG_FILE_DEFAULT'] = $aConfig['MVC_LOG_FILE_FOLDER'] . 'default.log';
$aConfig['MVC_LOG_FILE_ERROR'] = $aConfig['MVC_LOG_FILE_FOLDER'] . 'error.log';
$aConfig['MVC_LOG_FILE_WARNING'] = $aConfig['MVC_LOG_FILE_FOLDER'] . 'warning.log';
$aConfig['MVC_LOG_FILE_NOTICE'] = $aConfig['MVC_LOG_FILE_FOLDER'] . 'notice.log';
$aConfig['MVC_LOG_FILE_POLICY'] = $aConfig['MVC_LOG_FILE_FOLDER'] . 'policy.log';
$aConfig['MVC_LOG_FILE_EVENT'] = $aConfig['MVC_LOG_FILE_FOLDER'] . 'event.log';
// control log details
$aConfig['MVC_LOG_DETAIL'] = [
'date' => true,
'host' => true,
'env' => true,
'ip' => true,
'uniqueid' => true,
'sessionid' => true,
'count' => true,
'debug' => true,
'message' => true,
];
/**
* Caching
*/
// cache folder
$aConfig['MVC_CACHE_DIR'] = $aConfig['MVC_APPLICATION_PATH'] . '/cache';
$aConfig['MVC_CACHE_CONFIG'] = array(
'bCaching' => true,
'sCacheDir' => $aConfig['MVC_CACHE_DIR'],
'iDeleteAfterMinutes' => 1440,
'sBinRemove' => $aConfig['MVC_BIN_REMOVE'],
'sBinFind' => $aConfig['MVC_BIN_FIND'],
'sBinGrep' => $aConfig['MVC_BIN_GREP']
);
/**
* misc
*/
// Secure Port, SSL
$aConfig['MVC_SSL_PORT'] = 443;
// boolean Request is secure ? (SSL)
$aConfig['MVC_SECURE_REQUEST'] = (array_key_exists('HTTPS', $_SERVER) && strtolower($_SERVER['HTTPS']) !== 'off') || (array_key_exists('SERVER_PORT', $_SERVER) && ($_SERVER['SERVER_PORT'] == $aConfig['MVC_SSL_PORT']));
/**
* Session
*/
// session folder and
// Session options @see http://php.net/manual/de/session.configuration.php
$aConfig['MVC_SESSION_NAMESPACE'] = 'myMVC';
$aConfig['MVC_SESSION_PATH'] = $aConfig['MVC_APPLICATION_PATH'] . '/session';
$aConfig['MVC_SESSION_OPTIONS'] = array(
'cookie_httponly' => true,
'auto_start' => 0,
'save_path' => $aConfig['MVC_SESSION_PATH'],
'cookie_secure' => $aConfig['MVC_SECURE_REQUEST'],
'name' => 'myMVC' . (($aConfig['MVC_SECURE_REQUEST']) ? '_secure' : ''),
'save_handler' => 'files',
'cookie_lifetime' => 0,
// max value for "session.gc_maxlifetime" is 65535. values bigger than this may cause php session stops working.
'gc_maxlifetime' => 65535,
'gc_probability' => 1,
'use_strict_mode' => 1,
'use_cookies' => 1,
'use_only_cookies' => 1,
'upload_progress.enabled' => 1,
);
// default behaviour
// false: session won't start. This means NO cookie is written to client
// true: session will start
$aConfig['MVC_SESSION_ENABLE'] = false;
// Routing Class
$aConfig['MVC_ROUTING_CLASS'] = '\\MVC\\Routing';
// routing.json file
$aConfig['MVC_ROUTING_JSON'] = '';
// detect if request is done via cli. set boole true|false
$aConfig['MVC_CLI'] = (('cli' === php_sapi_name()) ? true : false);
}
MVC_TEMPLATE_ENGINE_SMARTY: {
$aConfig['MVC_SMARTY_CACHE_STATUS'] = false;
$aConfig['MVC_SMARTY_CACHE_DIR'] = $aConfig['MVC_APPLICATION_PATH'] . '/cache';
$aConfig['MVC_SMARTY_TEMPLATE_DIR'] = $aConfig['MVC_VIEW_TEMPLATES'];
$aConfig['MVC_SMARTY_TEMPLATE_DEFAULT'] = 'Frontend/layout/index.tpl';
// templates_c folder and
// templates_c folder access rights, octal mode
$aConfig['MVC_SMARTY_TEMPLATE_CACHE_DIR'] = $aConfig['MVC_APPLICATION_PATH'] . '/templates_c';
// array Location of Smarty PlugIns
$aConfig['MVC_SMARTY_PLUGINS_DIR'][] = $aConfig['MVC_APPLICATION_PATH'] . '/smartyPlugins';
}
MODULES: {
$aConfig['MODULE'] = array();
}
/**
* @see /application/doc/README "Policy"
* Policy Rules are bonded to a specific "Controller::Method"
* Define your policies
*/
MVC_POLICY: {
$aConfig['MVC_POLICY'] = array();
}
MVC_MISC: {
$aConfig['MVC_UNIQUE_ID'] = date('YmdHis') . '' . uniqid();
}
Example: /modules/Foo/etc/config/_mvc.php
<?php
//-------------------------------------------------------------------------------------
// Module
$aConfig['MVC_MODULE_CURRENT_NAME'] = 'Foo';
$aConfig['MVC_MODULE_CURRENT_DIR'] = $aConfig['MVC_MODULES_DIR'] . '/' . $aConfig['MVC_MODULE_CURRENT_NAME'];
$aConfig['MVC_MODULE_CURRENT_CONFIG_DIR'] = $aConfig['MVC_MODULE_CURRENT_DIR'] . '/etc/config';
$aConfig['MVC_MODULE_CURRENT_CONTROLLER_DIR'] = $aConfig['MVC_MODULE_CURRENT_DIR'] . '/Controller';
$aConfig['MVC_MODULE_CURRENT_DATATYPE_DIR'] = $aConfig['MVC_MODULE_CURRENT_DIR'] . '/DataType';
$aConfig['MVC_MODULE_CURRENT_ETC_DIR'] = $aConfig['MVC_MODULE_CURRENT_DIR'] . '/etc';
$aConfig['MVC_MODULE_CURRENT_STAGING_CONFIG_DIR'] = $aConfig['MVC_MODULE_CURRENT_CONFIG_DIR'] . '/' . $aConfig['MVC_MODULE_CURRENT_NAME'] . '/config';
$aConfig['MVC_MODULE_CURRENT_EVENT_DIR'] = $aConfig['MVC_MODULES_DIR'] . '/Event';
$aConfig['MVC_MODULE_CURRENT_MODEL_DIR'] = $aConfig['MVC_MODULES_DIR'] . '/Model';
$aConfig['MVC_MODULE_CURRENT_POLICY_DIR'] = $aConfig['MVC_MODULES_DIR'] . '/Policy';
$aConfig['MVC_MODULE_CURRENT_VIEW_DIR'] = $aConfig['MVC_MODULES_DIR'] . '/View';
$aConfig['MVC_MODULE_CURRENT_COMPOSER_DIR'] = $aConfig['MVC_MODULE_CURRENT_CONFIG_DIR'] . '/' . $aConfig['MVC_MODULE_CURRENT_NAME'];
//-------------------------------------------------------------------------------------
// MVC
// show InfoTool bar
$aConfig['MVC_INFOTOOL_ENABLE'] = true;
// override default fallback routing
$aConfig['MVC_ROUTING_FALLBACK'] = $aConfig['MVC_ROUTE_QUERY_PARAM_MODULE'] . '=' . $aConfig['MVC_MODULE_CURRENT_NAME'] . '&'
. $aConfig['MVC_ROUTE_QUERY_PARAM_C'] . '=index&'
. $aConfig['MVC_ROUTE_QUERY_PARAM_M'] . '=notFound';
// Smarty
$aConfig['MVC_VIEW_TEMPLATES'] = $aConfig['MVC_MODULE_CURRENT_DIR'] . '/templates';
$aConfig['MVC_SMARTY_TEMPLATE_DIR'] = $aConfig['MVC_VIEW_TEMPLATES'];
$aConfig['MVC_SMARTY_TEMPLATE_DEFAULT'] = 'Frontend/layout/index.tpl'; # relative path from templates folder
// Add Location of Smarty PlugIns
$aConfig['MVC_SMARTY_PLUGINS_DIR'][] = $aConfig['MVC_MODULE_CURRENT_ETC_DIR'] . '/smartyPlugins';
Example: /modules/Foo/etc/config/Foo/config/develop.php
<?php
use MVC\Config;
error_reporting(E_ALL);
date_default_timezone_set('Europe/Berlin');
// show InfoTool bar
$aConfig['MVC_INFOTOOL_ENABLE'] = true;
//-------------------------------------------------------------------------------------
// Module Foo
$aConfig['MODULE']['Foo'] = array();
//-------------------------------------------------------------------------------------
include '_session.php';
include '_csp.php';