install kohana website on multiple servers - php

I have a Kohana 3.0.14 website that i want to put on multiple domains, having associated a virtual host each (different ips).
the difference between my websites is the configuration file and the boostrap file (where i set the language to be used).
All the sites are in production.
How can i 'breakup' the website, how can i include the files so that i would have all the kohana site in a single place, and the config and boostrap on every server, so that when i am fixing an error to be fixed on every site (every domain)?
thanks a lot!

You could do that by setting up an environment variable at the top of you index.php file. Then depending on this variable, you'll set the configuration variables, languages, etc. This is usually how I handle staging/live/local environments, and doing so allows you to keep all the code identical between the various installations.
For example, in index.php:
define("ENV", "staging")
Then in bootstrap.php:
$baseUrl = "http://defaultdomain.com/";
if (ENV == "staging") $baseUrl = "http://staging.somedomain.com/";
Kohana::init(array(
'base_url' => $baseUrl,
));
In database.php:
if (ENV == "live") {
$hostname = ...
$database = ...
$username = ...
$password = ...
} else if (ENV == "staging") {
$hostname = ...
$database = ...
$username = ...
$password = ...
}
return array
(
'default' => array
(
'type' => 'mysql',
'connection' => array(
'hostname' => $hostname,
'database' => $database,
'username' => $username,
'password' => $password,
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
)

If your hosting options are limited, your best bet is to choose the first primary domain and create an account using that. Then park more domains on top. Then simply get the URL to decide what language etc you want to use.

Related

Codeigniter 4 possible to assign db_name(Database.php) in a variable or any other option

We want to connect one of too many databases as a default.
as per user requriment so we want take a database name in a variable or Global variable.
we are unable to use any variable in Database.php file
so guys help me for this problem.
view code here Codeigniter 4.0.0
It has been a while since I tried to do "weird" things like attempting to assign dynamic variables in configuration files so I'm not entirely sure if CodeIgniter supports it. However, even if it does, I would suggest using the function designed for these sort of things:
https://codeigniter4.github.io/userguide/database/connecting.html#connecting-with-custom-settings
$custom = [
'DSN' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
$db = \Config\Database::connect($custom);
I suppose, since all else is the same except for database name you could also access the database configuration property for your default database and use $custom = array_replace($configDb, ['database' => $_SESSION['db_name']) to change out the database name. Obviously check that db_name exists and isn't empty and all that jazz. You could even put this entire logic in to a class with db_name as a constructor so you don't have to keep doing this all the time.
E.g. (untested):
namespace App\Config;
use InvalidArgumentException;
class DatabaseChooser extends \Config\Database
{
protected $dbName;
public function __construct(string $dbName = '')
{
$this->dbName = $dbName;
$config = $this->buildArr();
return parent::connect($config);
}
private function buildArr()
{
if (empty($this->dbName)) {
throw InvalidArgumentException('dbName cannot be empty');
}
$default = $this->defaultGroup;
if (property_exists($this, $default) && is_array($this->$default)) {
return array_replace($this->$default, ['database' => $this->dbName]);
} else {
throw new Exception('Invalid configuration.');
}
}
}
I'm sure you've considered it, but CI4 isn't exactly production ready afaik. To be honest, I'd be using something like Laravel, Lumen, or Symfony which already have an extensive and tested codebase, as well as many compatible third party packages.

Laravel 4 Database configuration with $_ENV

I want to use Laravel Environment for my Database credential, I did this:
inside bootstrap\start.php :
$env = $app->detectEnvironment(array(
'local' => array('My_PC'),
'production' => array('server.example.com')
));
created .env.local.php on same directory where serve.php is, and inside this code:
return array(
'DATABASE_NAME' => 'laravel_db',
'DATABASE_USER' => 'root',
'DATABASE_PASSWORD' => '1234'
);
and inside app\config created a local\app.php file containing this code:
return array(
'debug' => true,
);
and inside the app\config\database.php for my secured mysql connection I did:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => $_ENV['DATABASE_NAME'],
'username' => $_ENV['DATABASE_USER'],
'password' => $_ENV['DATABASE_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
but I'm getting an error on the CLI:
{"error":{"type":"ErrorException","message":"Undefined index: DATABASE_NAME","file":"c:\\xampp\\htdocs\\Larave_project\\app\\config\\database.php","line":58}}
How to resolve this issue?
The hostname based env detection solution will only work on Unix type system.
It won't work on Windows.
In Laravel 4.2 you can detect environment this way:
$env = $app->detectEnvironment(function() {
if ($_SERVER['SERVER_NAME'] == "127.0.0.1") {
$domain = $_SERVER['HTTP_HOST'];
if (strpos($domain, 'localhost') !== FALSE || strpos($domain, "127.0.0.1") !== FALSE) {
die("Configure your local `hosts` file and go to address: http://{storeName}.local");
}
$len = strpos($domain, ".local");
if ($len !== FALSE) {
// will load .env.*.php
$len = strpos($domain, ".local");
$env = substr($domain, 0, $len);
return $env;
}
}
// will load .env.php
return 'production';
});
(this is bootstrap/start.php)
Then:
setup hosts file (in your operating system) to redirect mysuperstore.local to 127.0.0.1
go to URL like http://mysuperstore.local which loads .env.mysuperstore.php into $_ENV.
In configuration files (those placed in app/config/) refer to $_ENV. To see where $_ENV does come from take a look into documentation topic about "Protecting Sensitive Configuration".

Using different databases based on user request

I've installed Laravel-4 and jenssegers / Laravel-MongoDB which has the same interface to Eloquent model as Laravel, so everything is pretty transparent and 1 database connection works OK.
what I'm trying to do, is switch to another database based on user request (Consider it as API that decided where to go and grab data).
This is what I did:
App::before(function($request)
{
$dbPrefix = $request->segment(1);
if (!is_null($dbPrefix)) {
$dbName = strtolower($dbPrefix);
$newDb = DB::connection('mongodb_'.$dbName);
}
});
From here.. I don't know what to do.. Is it connected to new database that way? how do I tell my Laravel to use $newDb when I refer to DB constant in Models?
But I want it to happen before application starts, so specifying "$connection" variable in model or using explicit call to other database like DB::connection('mongodb2')->query(...) is no good for me.
Thanks
The solution to this would be:
/app/filters.php:
App::before(function($request)
{
$dbPrefix = $request->segment(1);
if (!is_null($dbPrefix)) {
$connectionName = 'mongodb_'.strtolower($dbPrefix);
Config::set('database.default', $connectionName);
}
});
/config/database.php:
'mongodb_soccer' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'username' => 'user',
'password' => 'password',
'database' => 'SoccerData'
),
'mongodb_tennis' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'username' => 'user',
'password' => 'password',
'database' => 'TennisData'
)
Requests:
site.com/soccer
will get connection mongodb_soccer
site.com/tennis
will get connection mongodb_tennis
You can pre-authorize it in default "admin" database where your users are stored, and then switch to any database connection per user request to get the actual data.
I needed it this way for API development.
Good luck

CakePHP - Setup database with a form

I would like to setup the database in CakePHP providing host, login and password with a form (so without writing the database.php file myself). The purpose is some kind of automatic setup of CakePHP (I would use this to provide salt and cypherSeed too).
Is this possible? What's the best way to do go? I read something about writing file via PHP...
If you create/load your database connection from your controller you can have this data variable. I don't think it's a good idea to write database.php file with a form.
So I would do something like this:
//Controller
$this->Formdatabase = ClassRegistry::init('Formdatabase');
$db_host = '';//load from file or DB
$db_user = '';//load from file or DB
$db_pass = '';//load from file or DB
$db_database = '';//load from file or DB
ConnectionManager::create("form_database", array(
'datasource' => 'Database/Mysql',
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_user,
'password' => $db_pass,
'database' => $db_database,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
));
$this->Formdatabase->useTable = ''; //table you want to use. (in Model or controller)
//Model
<?php
class Formdatabase extends Model {
public $useDbConfig = 'form_database';
}
?>
//Now you can use $this->Formdatabase->find('...'); ...
Hope I could help, good luck!

Cake php Datasource class MySQL could not be found

I have ubuntu 10.04 on server.
I am trying to set up the cake php project but it gives me following error
Cake is NOT able to connect to the database.
Datasource class MySQL could not be found.
I have searched lot on the web regarding it.
my config file looks like this
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/MySQL',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'mypassword',
'database' => 'dbname',
'prefix' => '',
//'encoding' => 'utf8',
);
}
I checked that server has all the things set up to connect as PDO I have run following script and it works fine.
$conn = new PDO('mysql:host=localhost;dbname=dbname', $username, $password);
Then further I have changed in Mysql.php file of cake php which is in the "lib\Cake\Model\Datasource\Database"
I tried to give static connection in Mysql.php but this also doesn't work. I did exit in the Mysql.php and seems like control of page is not getting here.
$this->_connection = new PDO('mysql:host=localhost;dbname=dbname', $username, $password);
$this->connected = true;
Please do let me know if I am missing anything.
Thanks in Advance.
Casing matters, it should be:
'datasource' => 'Database/Mysql'
And not:
'datasource' => 'Database/MySQL'
Mysql is not a supported source try 'datasource' => 'Database/Sqlite',

Categories