The Configuration component in Zemit Core is a flexible and powerful tool for managing and accessing your application's configuration settings. It provides a default class with predefined values that can be customized to fit your needs, and also allows you to load environment variables from a .env file. The Configuration component is decoupled into multiple sections, making it easy to organize and manage your settings. With the Configuration component, you can easily control and adjust your application's configuration within the Zemit Core framework.
Good to know: Zemit is using vlucas Dotenv library to load and prepare environment variable from the .env file, please refer yourself to the official Dotenv documentation.
Basic .env examples
# Set Application EnvironmentAPPLICATION_ENV=local# Enable DebugAPP_DEBUG=true# Enable ProfilerAPP_PROFILER=true# Enable Send EmailAPP_SEND_EMAIL=true# Use SMTPMAILER_DRIVER=smtpMAILER_SMTP_HOST=smtp.server.tldMAILER_SMTP_PORT=465MAILER_SMTP_ENCRYPTION=tlsMAILER_SMTP_USERNAME=usernameMAILER_SMTP_PASSWORD="smtp_password"MAILER_FROM_EMAIL=email@server.tldMAILER_FROM_NAME="App Name"# Enable APCU CacheAPP_CACHE=trueMETADATA_DRIVER=apcuCACHE_DRIVER=apcu# Set Global Security SaltSECURITY_SALT="my_app_salt"# Set Master Database ConfigurationDATABASE_HOST=localhostDATABASE_DBNAME=database_nameDATABASE_USERNAME=database_usernameDATABASE_PASSWORD="database_password"# Add a new Slave DatabaseDATABASE_READONLY_ENABLE=trueDATABASE_READONLY_HOST=slave.db.server.tld# Set Custom Authorization HeaderIDENTITY_AUTHORIZATION_HEADER=X-Authorization# Set LocalisationLOCALE_DEFAULT=frLOCALE_ALLOWED=fr,en,esLOCALE_MODE=session_geoipLOCALE_SESSION_KEY=my_app_localTRANSLATE_LOCALE=fr_CA.utf8TRANSLATE_DEFAULT_PATH=./app/Locales/# Set Redis ServerREDIS_HOST=redis.server.tldREDIS_PORT=6379
Advanced .env examples
Database Settings (Master & Slave)
Zemit currently only support MySQL/MariaDB database driver. There is no plan on adapting new databases adapters in the future. Since Phalcon does support many database adapters, nothing stops you from defining your own from your own configuration file.
Good to know: The Slave Database is only used as a read-only server and will automatically extend the master settings if they are not explicitly defined.
You can use the redis Driver for some components such as session, annotation, metadata and cache. By default, it will automatically retrieve the default Redis settings which is prefixed with REDIS_.
# Using Redis Server for sessionSESSION_DRIVER=redis# Using Redis Server for annotationsANNOTATIONS_DRIVER=redis# Using Redis Server for meta-dataMETADATA_DRIVER=redis# Using Redis Server for cacheCACHE_DRIVER=redis# Default Redis SettingsREDIS_HOST=127.0.0.1REDIS_PORT=6379
Good to know: It is also possible to overwrite the default redis configuration for each components if you would like to use distinct redis server per component.
# Forcing other Redis Settings# specifically for the `session`SESSION_DRIVER=redisSESSION_REDIS_DEFAULT_SERIALIZER=1SESSION_REDIS_LIFETIME=3600SESSION_REDIS_SERIALIZER=SESSION_REDIS_HOST=127.0.0.1SESSION_REDIS_PORT=6379SESSION_REDIS_INDEX=1SESSION_REDIS_AUTH=SESSION_REDIS_PERSISTENT=0SESSION_REDIS_SOCKET=
# Forcing other Redis Settings# specifically for the `cache`CACHE_DRIVER=redisCACHE_REDIS_DEFAULT_SERIALIZER=1CACHE_REDIS_LIFETIME=3600CACHE_REDIS_SERIALIZER=CACHE_REDIS_HOST=127.0.0.1CACHE_REDIS_PORT=6379CACHE_REDIS_INDEX=1CACHE_REDIS_AUTH=CACHE_REDIS_PERSISTENT=0CACHE_REDIS_SOCKET=
# Forcing other Redis Settings# specifically for the `metadata`METADATA_DRIVER=redisMETADATA_REDIS_DEFAULT_SERIALIZER=1METADATA_REDIS_LIFETIME=3600METADATA_REDIS_SERIALIZER=METADATA_REDIS_HOST=127.0.0.1METADATA_REDIS_PORT=6379METADATA_REDIS_INDEX=1METADATA_REDIS_AUTH=METADATA_REDIS_PERSISTENT=0METADATA_REDIS_SOCKET=
# Forcing other Redis Settings# specifically for the `annotation`ANNOTATION_DRIVER=redisANNOTATION_REDIS_DEFAULT_SERIALIZER=1ANNOTATION_REDIS_LIFETIME=3600ANNOTATION_REDIS_SERIALIZER=ANNOTATION_REDIS_HOST=127.0.0.1ANNOTATION_REDIS_PORT=6379ANNOTATION_REDIS_INDEX=1ANNOTATION_REDIS_AUTH=ANNOTATION_REDIS_PERSISTENT=0ANNOTATION_REDIS_SOCKET=
Create a new Config class which should look similar to this below:
// ./app/Bootstrap/Config.phpnamespaceApp\Bootstrap;useZemit\Utils\Env;classConfigextendsZemit\Bootstrap\Config{publicfunction__construct(array $data = [],bool $insensitive =true) {parent::__construct(['app'=> [// override whatever variable you want'version'=>Env::get('APP_VERSION','0.0.1')// add custom configs wherever you want'customKey'=>Env::get('APP_CUSTOM_KEY','default-value')// ... ],// creating a new custom config'custom'=> ['key'=>Env::get('CUSTOM_KEY','default-value')// ... ], ], $insensitive);if (!empty($data)) {$this->merge(newPhalconConfig($data, $insensitive)); } }}
Once you have your custom Config class created, the bootstrap needs to be aware. To achieve this, we have multiple ways of doing that.
Method #1: Loading the custom Config Class from Bootstrap
You can refer yourself to the Bootstrap documentation method #1 (Using Bootstrap Variables). This is the preferred method if you want to avoid making a custom Service Provider for the Config Class
Method #2: Loading using a custom Service Provider
If you wish to load the custom Config Class using from the service provider which is the recommended way. You will have to create your own Service Provider as shown below, and then load it from the Bootstrap using Bootstrap documentation method #2 (Using Bootstrap Service Providers).
// ./app/Provider/Config/ServiceProvider.phpnamespaceApp\Provider\Config;classServiceProviderextendsZemit\Provider\AbstractServiceProvider{protected $serviceName ='config';/** * {@inheritdoc} * * @paramDiInterface $di */publicfunctionregister(DiInterface $di =null):void {// Set shared service in DI $di->setShared($this->getName(),function() use ($di) {/** @varBootstrap $bootstrap */ $bootstrap = $di->get('bootstrap');// Load your own custom Config // Or the one which was provided from the $bootstrap->config variable $config = $bootstrap->config ??new\App\Bootstrap\Config();if (is_string($config)&&class_exists($config)) { $config =new$config(); }// Set bootstrap mode into config $config->mode = $bootstrap->getMode();// Merge config with current environment $config->mergeEnvConfig();// Launch bootstrap prepare raw php configs $bootstrap->prepare->php($config->path('app'));// Set the configreturn $config; }); }}
Then you will have to map your own custom ServiceProvider to the Bootstrap like below: