Multiple systems/database using a single CodeIgniter installation

#1
[eluser]FernandoMM[/eluser]
Hello,

I have developed a basic sales system with CI. Currently, i have 5 clients using it and have 5 installations of the same app. All installations are the same, the only thing that differs them are the database that they are accessing.

Is there a easy way to have only one installation that recognize which client is accessing and connect to the correct database?

I was wondering if i could create URLS like:

Code:
http://my-site.com/client_name1/index.php/controller/blablabla

http://my-site.com/client_name2/index.php/controller/blablabla

But redirecting to the same CI installation ( this way i would keep how clients access today ).

Thanks,

Fernando.

#2
[eluser]DougW[/eluser]
I did this with symlinks for all system directories except for Applications. But then I switched to Mosso cloud computing. Now I have the same problem. I want to deploy several sites but with one main codeigniter set of code, except for the Applications folder.

I adjusted the system_folder and application_folder in /index.php. What I get is the firefox message:

"Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Both have been triple-checked to be accurate.

Ideas anyone?

#3
[eluser]Jelmer[/eluser]
FernandoMM:
You could point each index.php to the same system & application directory by setting $system_folder & $application_folder variables.

I've solved selecting the right database by setting a constant in each index.php, like this:
Code:
define('DATABASE_NAME', 'application_1');

And then selecting the right database by adding this code to the database.php config file.
Code:
$active_group = DATABASE_NAME;
And adding settings for each database with the different DATABASE_NAMEs as array keys.

The only thing left to solve is setting the right basepath in your config.php config file, which you could do by using the database_name:
Code:
if (DATABASE_NAME == 'application_1') $config['base_url'] = 'http://mydomain/application_1/';
elseif (DATABASE_NAME == 'application_2') $config['base_url'] = 'http://mydomain/application_2/';

DougW:
That error is probably caused by either your application redirecting to itself or a htaccess redirect. Such an error wouldn't be caused by the location of your system and application folders.

#4
[eluser]DougW[/eluser]
Except that everything worked before changing the system_folder and application_folder. When I have these two on totally different paths, is there anythign else I need to change? Such as base_url?

#5
[eluser]Jelmer[/eluser]
The base URL might be the problem, especially if you're running an application in different instances from different locations. The base URL should be different for each instance and if it's wrong that might cause problems. Though such problems still aren't caused by CI itself, the redirect loop would be somewhere in your implementation.

The point is that your server doesn't care where the code is located, as long as it can find the code - though the basepath for relative file operations from within your code would be relative to the location of your index.php. Such an error isn't caused by loading your code from a different location. It's caused by a continues redirect which is most likely caused by your .htaccess or your application. In your case I'd try disabling my .htaccess first and accessing the index.php file directly. Which would break the continues redirect if it's caused by the htaccess, or you could see where your code redirects to because that might not work anymore after the htaccess is disabled. If that still redirects like crazy, start checking your code for the location of a redirect that could trigger itself over and over again.

#6
[eluser]DougW[/eluser]
Thanks. I'll give it a shot.

My directories are:

system_folder: /Users/fred/Sites/codeigniter/system
application_folder: /Users/fred/Sites/mysite/system/application

does the application folder HAVE to be in the sub-tree of CI?

I usually set by base_url to $_SERVER['HTTP_HOST'] so I don't have to worry about moving the site around.

When I had system folder set to system (under the docroot) everything worked fine. I am just truing to get both moved outside of docroot and be able to share the main CI directory amongst multiple sites..

#7
[eluser]Jelmer[/eluser]
The application dir doesn't have to be in the subtree of the system dir, just remember that the $application_folder is set relative to your system folder. So in your case, if you had:
system_folder: /Users/fred/Sites/system
application_folder: /Users/fred/Sites/mysite/application
example index.php location: /Users/fred/Sites/mysite/http_docs/index.php

You'd set the 2 variables to:
Code:
$system_folder = '../../system';
$application_folder = '../mysite/application';

Setting the base_rul by $_SERVER[‘HTTP_HOST’] should work fine as long as each site is on a domain, subdomain or IP address. When a site is in a subdirectory it wouldn't, because that subdirectory wouldn't be set. But though it might be part of your problem I don't think that your base_url is what's causing the redirect loop.

Quote:When I had system folder set to system (under the docroot) everything worked fine. I am just truing to get both moved outside of docroot and be able to share the main CI directory amongst multiple sites..
That shouldn't be a problem, I've got both my system and application dirs outside the document root and I'm running multiple sites using them. I'd start testing for the location of the error if I were you, maybe you've edited some of the files in the system dir?

#8
[eluser]DougW[/eluser]
Actually, you were right. I have a goofy loop in my user class due to the fact that the Mosso cloud does NOT auto-start sessions in php. I got the system_folder and application_folder working with both relative paths and absolute paths.

Along this line, As the owner of this thread asked (I believe) I have several sites where I want to share system folder and MOST of the code in the application folder. I just want separate database connections. What I did was make a separate file with the database connection variables as defines, store one of these with each site, then modify database.php to include that file. Works like a charm if you are doing what I am which is offering white-label versions of your application, each with it's own database. With your solution above, I would soon run out of database definitions.

Thanks for your help!

ERdit.

I was wrong. Includes don't work. Your method above does. Thanks!

#9
[eluser]DougW[/eluser]
I found an easier method. It only requires one change to database.php and then a custom index.php for each slave copy of your application.

In index.php, define four variables (more if you want to change more)


define('DATABASE_HOST', 'localhost');
define('DATABASE_USER', 'yourname');
define('DATABASE_PASS', 'yourpass');
define('DATABASE_NAME', 'database');

Then, leave your default database alone, just sent the array variables to the define variables as above...


$db['default']['hostname'] = DATABASE_HOST;
$db['default']['username'] = DATABASE_USER;
$db['default']['password'] = DATABASE_PASS;
$db['default']['database'] = DATABASE_NAME;

All I have to do to spawn an instance of my application is ensure that the system_folder and application_folder variables point to the right places and set the database variables in index.php.

Even config.php is handled by setting base_url as follows:

$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];

Basically, you can have multiple copies of your application by only changing the index.php file.

Hope this helps!

Doug


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.