Local development: environment configuration support?
Hi!
I would like to know if it's possible to set some configurations variables depending the enviroment in wich Vanilla is executed. I would like to set some vars on my local enviroment and others in production. I don't want to have two config.php files duplicated, one in my local enviroment and another one in production server.
Thanks!
Best Answer
-
Linc Admin
First I want to note that everything I'm about to describe is fairly advanced. Anyone wandering into this discussion should seriously consider whether they need this. I doubt it is worth the trouble for most folks; just use your configs normally and manually sync something when you must.
@nikoskip said:
I don't want to have two config.php files duplicated, one in my local enviroment and another one in production server.That, however, is exactly what you need to do. There are a number of security-related settings in the config that should not be duplicated between development and production environments.
You can use hooks to forcibly set certain config settings depending on the environment if you want.
I use a
bootstrap.early.php
in my/conf
folder on localhost to set certain configs:<?php if (!defined('APPLICATION')) exit(); if (c('Garden.Installed')) { // You could set some other environment-specific criteria here if you want. saveToConfig('DebugAssets', true); saveToConfig('Debug', true); saveToConfig('Garden.Cdns.Disable', true); // and so on. }
I would use this very conservatively; not for every config setting.
To use this in production, you'd want to add a flag to
saveToConfig()
to disable config writes and only save to memory (create a third argument set tofalse
). Attempting to write to the physical file in the bootstrap could be extremely bad depending on the scale of your site.You could alternatively do this via plugin or theme hooks, depending on context.
6
Answers
typically this is done with a deployment flow. Such as if you used github and springloops.
config would be excluded from this using .gitignore as would cache and uploads
grep is your friend.
First I want to note that everything I'm about to describe is fairly advanced. Anyone wandering into this discussion should seriously consider whether they need this. I doubt it is worth the trouble for most folks; just use your configs normally and manually sync something when you must.
That, however, is exactly what you need to do. There are a number of security-related settings in the config that should not be duplicated between development and production environments.
You can use hooks to forcibly set certain config settings depending on the environment if you want.
I use a
bootstrap.early.php
in my/conf
folder on localhost to set certain configs:I would use this very conservatively; not for every config setting.
To use this in production, you'd want to add a flag to
saveToConfig()
to disable config writes and only save to memory (create a third argument set tofalse
). Attempting to write to the physical file in the bootstrap could be extremely bad depending on the scale of your site.You could alternatively do this via plugin or theme hooks, depending on context.
@x00 yeah, I'm using Git and I'm currently excluding
cache
anduploads
folders and was looking a way to have one base config file and set some configs according the enviroment.@Linc thanks! I could really get use of that approach. I'm very used to work with Symfony and Laravel, and environments configuration is common, that's why I'm asking.
Thanks both of you, guys!
I've used saveToConfig even on the theme level though it depends how early the config is required and the hook you use.
bootstrap.early.php works well though you only want to save to config one better to use save=false after the config file is loaded that way you are loading on the fly. No need to write to file.
grep is your friend.
@Linc I'm trying to set the database configuration using the
bootstrap.early.php
approach but is not working as I expected, for instance:1.- If I comment the database config section from the
config.php
file it works and Vanilla gets connected, but... I can't do any change that affects theconfig.php
file, like activate/deactivate plugins. Nothing get saved on the file.2.- If I just leave in blank the database configuration, instead of commented it, Vanilla can't connects, it's like it is reading the configuration directly from the file instead of memory.
Thanks!
The config and the database both get loaded in the main bootstrap sequence without any intervening bootstrap.x moment being called, so I don't think it's possible to override that one via this method in the current release.