Last active
December 16, 2015 10:49
-
-
Save prbaron/5423399 to your computer and use it in GitHub Desktop.
Improved database.php file for CakePHP
This file chooses the good database configuration whether you are in local or production mode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is core configuration file. | |
* | |
* Use it to configure core behaviour of Cake. | |
* | |
* PHP 5 | |
* | |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
* | |
* Licensed under The MIT License | |
* Redistributions of files must retain the above copyright notice. | |
* | |
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
* @link http://cakephp.org CakePHP(tm) Project | |
* @package app.Config | |
* @since CakePHP(tm) v 0.2.9 | |
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
*/ | |
/** | |
* In this file you set up your database connection details. | |
* | |
* @package cake.config | |
*/ | |
/** | |
* Database configuration class. | |
* You can specify multiple configurations for production, development and testing. | |
* | |
* datasource => The name of a supported datasource; valid options are as follows: | |
* Database/Mysql - MySQL 4 & 5, | |
* Database/Sqlite - SQLite (PHP5 only), | |
* Database/Postgres - PostgreSQL 7 and higher, | |
* Database/Sqlserver - Microsoft SQL Server 2005 and higher | |
* | |
* You can add custom database datasources (or override existing datasources) by adding the | |
* appropriate file to app/Model/Datasource/Database. Datasources should be named 'MyDatasource.php', | |
* | |
* | |
* persistent => true / false | |
* Determines whether or not the database should use a persistent connection | |
* | |
* host => | |
* the host you connect to the database. To add a socket or port number, use 'port' => # | |
* | |
* prefix => | |
* Uses the given prefix for all the tables in this database. This setting can be overridden | |
* on a per-table basis with the Model::$tablePrefix property. | |
* | |
* schema => | |
* For Postgres specifies which schema you would like to use the tables in. Postgres defaults to 'public'. | |
* | |
* encoding => | |
* For MySQL, Postgres specifies the character encoding to use when connecting to the | |
* database. Uses database default not specified. | |
* | |
* unix_socket => | |
* For MySQL to connect via socket specify the `unix_socket` parameter instead of `host` and `port` | |
*/ | |
class DATABASE_CONFIG { | |
#localhost | |
var $local = array( | |
'datasource' => 'Database/Mysql', | |
'persistent' => false, | |
'host' => 'localhost', | |
'login' => 'login', | |
'password' => 'password', | |
'database' => 'database_name', | |
'prefix' => '', | |
'encoding' => 'utf8', | |
); | |
#live server | |
var $live = array( | |
'datasource' => 'Database/Mysql', | |
'persistent' => false, | |
'host' => 'live_host', | |
'login' => 'live_login', | |
'password' => 'live_password', | |
'database' => 'live_database_name', | |
'prefix' => '', | |
'encoding' => 'utf8', | |
); | |
#switch between configs | |
var $default = array(); | |
var $test = array(); | |
function __construct() { | |
#wildcard the subdomains | |
$host_r = explode('.', $_SERVER['SERVER_NAME']); | |
if(count($host_r)>2) while(count($host_r)>2)array_shift($host_r); | |
$mainhost = implode('.', $host_r); | |
#switch between servers | |
switch(strtolower($mainhost)) { | |
case 'localhost': | |
$this->default = $this->local; | |
break; | |
case '{your live url}': | |
$this->default = $this->live; | |
break; | |
default: | |
$this->default = $this->local; | |
} | |
} | |
#php 4 compatibility | |
function DATABASE_CONFIG() { | |
$this->__construct(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment