Last active
November 27, 2019 09:10
-
-
Save samuraee/9364705 to your computer and use it in GitHub Desktop.
Environment based configuration for PhalconPHP (Production/Development/Staging)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// your main aplication config file | |
// app/config/application.php | |
$config = [ | |
'application' => [ | |
'cacheDir' => __DIR__ . '/../../cache/', | |
'appDir' => __DIR__ . '/../', | |
'baseUri' => '/', | |
'basePath' => '/', | |
'publicUrl' => 'http://www.your_domain.com', | |
'cryptSalt' => '$9diko$.f#11', | |
], | |
'database' => [ | |
'adapter' => 'Mysql', | |
'host' => 'localhost', | |
'username' => 'prod_db_user', | |
'password' => 'prod_db_pass', | |
'name' => 'prod_db_name', | |
'charset' => 'utf8', | |
], | |
'redis' => [ | |
'scheme' => 'tcp', | |
'host' => '127.0.0.1', | |
'port' => 6379 | |
], | |
'view' => [ | |
'layoutsDir' => '/../../../layouts/', | |
'partialsDir' => '/../../../partials/', | |
'viewsDir' => '/../../../views/', | |
'defaultLayout' => 'main', | |
], | |
'mail' => [ | |
'fromName' => 'Your App Postmaster', | |
'fromEmail' => 'noreply@prod_domain.com', | |
'smtp' => [ | |
'server' => 'smtp.mandrillapp.com', | |
'port' => 587, | |
'security' => 'tls', | |
'username' => 'USERNAME', | |
'password' => 'PASSWORD', | |
], | |
'mandrill_api_key' => 'YOUR_MANDRILL_API_KEY', | |
'amazon' => [ | |
'AWSAccessKeyId' => "AMAZONE_KEY", | |
'AWSSecretKey' => "AMAZON_SECRET" | |
], | |
], | |
'oAuth' => [ | |
"GitHub" => [ | |
'key' => 'KEY', | |
'secret' => 'SECRET', | |
], | |
"google" => [ | |
'key' => 'KEY', | |
'secret' => 'SECRET', | |
] | |
] | |
]; | |
function array_merge_recursive_replace() | |
{ | |
$arrays = func_get_args(); | |
$base = array_shift($arrays); | |
foreach ($arrays as $array) { | |
reset($base); | |
while (list($key, $value) = @each($array)) { | |
if (is_array($value) && @is_array($base[$key])) { | |
$base[$key] = array_merge_recursive_replace($base[$key], $value); | |
} else { | |
$base[$key] = $value; | |
} | |
} | |
} | |
return $base; | |
} | |
// override production config by enviroment config | |
$config = file_exists(dirname(__FILE__). DIRECTORY_SEPARATOR . APPLICATION_ENV .'.php') | |
? array_merge_recursive_replace($config, require(APPLICATION_ENV .'.php')) | |
: $config; | |
return new \Phalcon\Config($config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Override production configs for development environment | |
// app/config/development.php | |
return [ | |
'application' => [ | |
'publicUrl' => '', | |
], | |
'database' => [ | |
'host' => 'localhost', | |
'username' => 'dev_db_user', | |
'password' => 'dev_db_pass', | |
'name' => 'dev_db_name', | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// public/index.php | |
defined('APPLICATION_ENV') || | |
define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); | |
// ADD "SetEnv APPLICATION_ENV development" to your Apache virtualhost | |
//Or | |
// ADD "fastcgi_param APPLICATION_ENV production;" to your nginx virtualhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Override production configs for staging environment | |
// app/config/staging.php | |
return [ | |
'application' => [ | |
'publicUrl' => '', | |
], | |
'database' => [ | |
'host' => 'localhost', | |
'username' => 'stg_db_user', | |
'password' => 'stg_db_pass', | |
'name' => 'stg_db_name', | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Aboozar,
I like your example, however I think that there's a better way of merging multiple different configurations already built in \Phalcon\Config - have a look at Merging Configurations.
Here is one example that I'm often using:
And all configuration will return \Phalcon\Config instead of array
My apologies if this functionality was not available back then