How do I configure CakePHP to detect my development / production environments or do I just keep the config files out of version control?
Solution 1
If I understood the question correctly, this might be the thing you need:
Automatically choose database connections in CakePHP
Briefly, override the DATABASE_CONFIG
constructor:
class DATABASE_CONFIG
{
//initalize variable as null
var $default=null;
//set up connection details to use in Live production server
var $prod =
array(
// ...
);
// and details to use on your local machine for testing and development
var $dev =
array(
// ...
);
function __construct ()
{
if(isset($_SERVER['SERVER_NAME'])){
switch($_SERVER['SERVER_NAME']){
case 'digbiz.localhost':
$this->default = $this->dev;
break;
case 'digbiz.example.com':
$this->default = $this->prod;
break;
}
}
else // we are likely baking, use our local db
{
$this->default = $this->dev;
}
}
}
Solution 2
It might be better to define the prod/dev in bootstrap.php. That way you can reuse the constant across the app.
bootstrap.php:
define('IS_PROD',(isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME'] == 'cubismedia.com') ? true : false);
database.php:
function __construct() {
if (IS_PROD) {
$this->default = $this->prod;
} else {
$this->default = $this->dev;
}
}
//development
public $dev = array(
//db settings
);
//production
public $prod = array(
//db settings
);
Solution 3
For the database, you could set up the local and production connection settings and then create the file app/app_model.php and include the line:
class AppModel extends Model
{
var $useDbConfig = 'production';
}
Then you could create the file app/app_controller.php and parse the domains and tell it which dbconfig to use and any other core.php settings by using Configure::write.
Solution 4
You could just create an extra file that holds the single Configure::write('debug', 0)
* directive, import that in the core.php file and keep it out of version control. Best of both worlds.
*or whatever else you need to change
nutsmuggler
Humanist with a twist + Irish fiddle player. iOs Designer + developer. Ruby on railler yet often recalcitrant php coder. Original Developer of the Wordpress Events Manager plugin.
Updated on June 05, 2022Comments
-
nutsmuggler almost 2 years
I am a RoR developer, but I am presently working with CakePHP for a project where I cannot tweak the server. In my Rails production server, Passenger is setup to automatically use the "Production" environment, overriding the local app setup. I would like to setup my cakephp app to do the same thing. How do you usually accomplish that with CakePHP? Do you set up a domain parser, or you just keep core.php and database.php out of version control? Thanks in advance, Davide
-
Eddie almost 15 yearsGLad this article helped you :) please leave feedback
-
BMitch over 11 yearsLink no longer works. In the future, it's a good idea to quote the important details from a link just in case this happens.
-
dr Hannibal Lecter over 11 years@BMitch: You are correct, it's a very good idea. I hope this is just a temporary error.
-
dr Hannibal Lecter over 11 years@BMitch: I've updated the answer now that Eddie's site is back online.
-
James Alday about 10 yearsThis is basically what I do, but I do it up a level in core.php. I set a constant there called "APPLICATION_ENV" so I can reference it elsewhere in the code and also load an environment specific config file where I define constants (config values would work too) for things like database host, api keys, etc.
-
deval almost 6 yearswhich one is better? put it in bootstrap or core? pro/cons?