Welcome Guest, Not a member yet? Register   Sign In
Multiple sites, multiple config files, one app / system
#11

[eluser]CroNiX[/eluser]
Had an idea. What if you defined your database config array in index.php so it would be unique for each site. Then, don't autoload the database. Instead autoload a library, or create a MY_Controller, and in the construct manually load the database and use that config array, which will be globally accessible since it's defined in index.php?
#12

[eluser]Bhashkar Yadav[/eluser]
you can use CMS Expression Engine which uses CodeIgniter library and developed by EllisLab. you can manage Multiple sites very easily.
#13

[eluser]@robertotra[/eluser]
You might use the database groups in such a away: in the common database config file (one for all clients) put


Code:
$active_group = 'default';
$active_record = TRUE;  // FALSE if you prefer to write your own plain SQL queries, as I do :)

// array of different clients
$customer = array('client1' => 'database1', 'client2' => 'database2', 'client3' => 'database3');

// array of common settings keys
$db_keys = array('hostname', 'username', 'password', 'dbdriver', 'dbprefix', 'pconnect', 'db_debug', 'cache_on', 'cachedir', 'char_set', 'dbcollat', 'swap_pre', 'autoinit', 'strictcon')

// common settings for all clients

$db['default']['hostname'] = 'your_mysql_server';
$db['default']['username'] = 'your_mysql-username';
$db['default']['password'] = 'your-mysql-password';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

// set custom settings

$db['default']['database'] = 'default-database';

foreach ($customer as $client => $clientDB)
{
    $db[$client]['database'] = $clientDB;

    foreach ($db_keys as $key)
    {
        $db[$client][$key] = $db['default'][$key];
    }
}


At this point, in your controllers or modules you can load the client's database by specifying his group:

Code:
$this->load->database('client1');

If all clients databases share the same tables structure, in your default-database you might duplicate that structure but putting only one row per table with some default id's values so you can show default error messages in case you forgot to specify the group name for a customer. If not, you should check tables existence before sending queries to the databases.

Of course this solution can work also for other settings to be changed over customers (with few changes to the '$customer' array) as the MySQL server itself (if you have more, or if some customer have their own data servers) or the default charset (in case of multilingual websites) and so on.

Hope this helps
Bye
Roberto
#14

[eluser]olimortimer[/eluser]
I've picked this project up again, and still tearing my hair out...

What I want to achieve is to have a config.php and database.php file completely outside the application folder, where you specify the settings for that individual website - basically how WordPress works. I don't want to have to add multiple client setups to the application/config/database.php file, I simply want to upload a config.php and database.php into the client's web directory, and for CI to pick that up.

So this structure:

/application
-/config.php (default)
-/database.php (default)
/system

-/client1
--/index.php (client1 specific)
--/config.php (client1 specific)
-/database.php (client1 specific)

-/client2
--/index.php (client2 specific)
--/config.php (client2 specific)
-/database.php (client2 specific)


BUT, I don't want to use symlinks, as I want the project to be completely portable.

Using the index.php file, I can assign config items using $assign_to_config['X'] which is great! However, can I do the same with the database settings?
#15

[eluser]olimortimer[/eluser]
So I think I might have cracked it, but wanted to get people's opinions on whether or not this is ok to do, and if it will effect anything else...

I've created application/core/MY_Loader.php which is a direct copy of the database function in the Loader class, but with one extra line:

Code:
include_once(FCPATH.'config/config.php');

This loads the config file, and the $db variables, which then get passed into the DB class on the next line.

I did create a new function, called it in the construct, included the config file then passed the parameters to $this->database(), but it seemed to give some odd errors back when I gave it an incorrect database (testing to see if it told me it couldn't connect).

So MY_Loader.php is:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

    function __construct()
    {
        parent::__construct();
    }

public function database($params = '', $return = FALSE, $active_record = NULL)
{

  // Grab the super object
  $CI =& get_instance();

  // Do we even need to load the database class?
  if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
  {
   return FALSE;
  }

  require_once(BASEPATH.'database/DB.php');

  if ($return === TRUE)
  {
   return DB($params, $active_record);
  }

  // Initialize the db variable.  Needed to prevent
  // reference errors with some configurations
  $CI->db = '';

  // Load the site's custom config file,
  // and assign the db params
  include_once(FCPATH.'config/config.php');

  // Load the DB class
  $CI->db =& DB($db, $active_record);
}
}

/* End of file MY_Loader.php */
/* Location: ./system/application/core/MY_Loader.php */

Now on to a similar way to autoload the normal config items...
#16

[eluser]Matalina[/eluser]
Not sure this has been suggested or not. I know I see mention of multiple environments.

Folder structure
- ci
--application folder
---config
---- client1 folder
----- client only configs here
---- client 2 folder
-----client only configs here
---- all default configs here
-- system folder
-- client1 folder
---index.php (only)
-- client 2 folder
--- index.php(only)

In index.php for each site:
Code:
define('ENVIRONMENT', 'development');
$system_path = 'system';
$application_folder = 'application';

Change the environment to client1
Change the system page to ../system
change the application folder to ../application

Or the full paths

Inside of application/config create client 1folder and client2 folder but all database and client specific configs in there.

Have each client site point to the ci/client1 folder or have their domain point that folder
#17

[eluser]Bui Duc Long[/eluser]
still not resolved the question: how to setup 1 CI for multiple domain?
#18

[eluser]WanWizard[/eluser]
[quote author="Bui Duc Long" date="1339661573"]still not resolved the question: how to setup 1 CI for multiple domain?[/quote]
How I do it:

every domain has it's own DOCROOT. it contains the index.php and the application folder for the application running on that domain.

I have a shared location, outside all docroots, that contains the CI system folder (I have several versions online this way). In the index.php I have changed the location of the system to reflect this layout.

I think the suggestion to add multiple application into a single application is a very bad idea. You make it very complex to keep separation, you can have class conflicts, and from a development point of view it will be very complex to manage.

I prefer this to the way it's suggested in the documentation, altough that's the only way to solve it if you only have a single DOCROOT available (like with a shared hosting solution) and you want to run multiple applications.

In that case, follow the docs (create application/app1/... and application/app2/...), copy the index.php to app1.php and app2.php, and update them to point to the correct application folder. Then add a .htaccess file that rewrites to either app1.php or app2.php based on hostname requested. After this you can point multiple hostnames to the same docroot.
#19

[eluser]Bui Duc Long[/eluser]
Hi WanWizard,

Could you share your htaccess file that rewrite app1.php based on hostname request?




Theme © iAndrew 2016 - Forum software by © MyBB