2

We have a project setup which has three different git branches (development, staging and production). We are trying to find the best way to setup (if possible) the configuration.php for each branch.

One approach we took was to create add a constructor on the configuration.php and check the $_SERVER['DOCUMENT_ROOT'] and change the property for each branch/environment. This works until someone the makes changes in the backend and saves it which removes the constructor and sets it back to how the default configuration.php structure is.

Is there a way without hacking the core we can have environment specific configuration.php file or is the best way simply to put the configuration.php into .gitignore?

6

Regarding how Joomla loads configuration.php in index.php, at line 29

if (file_exists(__DIR__ . '/defines.php'))
{
    include_once __DIR__ . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}

You can put a custom defines.php in the Joomla root directory, then customize the 'JPATH_CONFIGURATION' value, something like this:

<?php
/**
 * @package    Joomla.Site
 *
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

define('_JDEFINES', 1); 
define('JPATH_BASE', dirname(__FILE__));

$branch = $_REQUEST["branch"];

// Global definitions
$parts = explode(DIRECTORY_SEPARATOR, JPATH_BASE);

// Defines.
define('JPATH_ROOT',          implode(DIRECTORY_SEPARATOR, $parts));
define('JPATH_SITE',          JPATH_ROOT);
define('JPATH_CONFIGURATION', JPATH_ROOT . DIRECTORY_SEPARATOR .'configurations' . DIRECTORY_SEPARATOR .$branch);
define('JPATH_ADMINISTRATOR', JPATH_ROOT . DIRECTORY_SEPARATOR . 'administrator');
define('JPATH_LIBRARIES',     JPATH_ROOT . DIRECTORY_SEPARATOR . 'libraries');
define('JPATH_PLUGINS',       JPATH_ROOT . DIRECTORY_SEPARATOR . 'plugins');
define('JPATH_INSTALLATION',  JPATH_ROOT . DIRECTORY_SEPARATOR . 'installation');
define('JPATH_THEMES',        JPATH_BASE . DIRECTORY_SEPARATOR . 'templates');
define('JPATH_CACHE',         JPATH_BASE . DIRECTORY_SEPARATOR . 'cache');
define('JPATH_MANIFESTS',     JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'manifests');

Creating

  • configurations/development/configuration.php
  • configurations/staging/configuration.php
  • configurations/production/configuration.php

UPDATE

For the administrator, you must create another defines.php in the /administrator directory.

<?php
/**
 * @package    Joomla.Administrator
 *
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

define('_JDEFINES', 1); 
define('JPATH_BASE', dirname(__FILE__));

$branch = $_REQUEST["branch"];

// Global definitions
$parts = explode(DIRECTORY_SEPARATOR, JPATH_BASE);
array_pop($parts);

// Defines
define('JPATH_ROOT',          implode(DIRECTORY_SEPARATOR, $parts));
define('JPATH_SITE',          JPATH_ROOT);
define('JPATH_CONFIGURATION', JPATH_ROOT . DIRECTORY_SEPARATOR .'configurations' . DIRECTORY_SEPARATOR .$branch);
define('JPATH_ADMINISTRATOR', JPATH_ROOT . DIRECTORY_SEPARATOR . 'administrator');
define('JPATH_LIBRARIES',     JPATH_ROOT . DIRECTORY_SEPARATOR . 'libraries');
define('JPATH_PLUGINS',       JPATH_ROOT . DIRECTORY_SEPARATOR . 'plugins');
define('JPATH_INSTALLATION',  JPATH_ROOT . DIRECTORY_SEPARATOR . 'installation');
define('JPATH_THEMES',        JPATH_BASE . DIRECTORY_SEPARATOR . 'templates');
define('JPATH_CACHE',         JPATH_BASE . DIRECTORY_SEPARATOR . 'cache');
define('JPATH_MANIFESTS',     JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'manifests');

define('_JDEFINES', 1); line prevents Joomla loads core includes/defines.php file.

$branch can be defined regarding $_REQUEST, catching subdomain or whatever you want.

  • Unless I am doing something wrong, this method worked fine until I saved the configuration from the backend and instead of overwriting the /configurations/development/configuration.php as I had expected (since I was saving in the development), it still overwrites the /configuration.php. I used $_REQUEST['SERVER_NAME'] for the $branch. – sifu May 2 '16 at 0:37
  • 1
    I'm sorry, I forgot the administrator part, It's the same as frontend, check the update. – José Sánchez Martín May 2 '16 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.