Setting up the environment for testing and production

I am developing a system in CakePHP using Git as a version control system. I have a working copy on my test server, and another on my working server, as with different databases. Every time I make changes, I need to change the database configuration so that I can test the system. Is there another way to save two files with different contents, one in the test and the other on the production server? Are affiliates a good way to go?

+3
source share
3 answers

I went for a rather crude but effective technique: in my development environment, I have an empty file called "environment_development". In my production environment, I call "environment_PRODUCTION" (another case for adding visual attention). My gitignore file is set to ignore both of these.

The front controller of my application (I use the Kohana framework, but I assume CakePHP has something similar) checks for these files and sets the IN_PRODUCTION constant accordingly. The rest of the code (database configuration, error handling, etc.) can check the value of this constant and change the behavior as necessary.

I used the check $ _SERVER ['SERVER_NAME'], but this method has the following advantages:

  • , , , cronjob, $_SERVER .
  • , .
  • : - , , , ( ) , - .
+1

. PHP , , ( ) , , , .

( Rails, .)

+4

If the database is environmentally dependent, you can do something like this in a file database.php:

class DATABASE_CONFIG {

    var $default = NULL;

    var $prod = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'username',
        'password' => 'password',
        'database' => 'productionDatabaseName',
        'prefix' => '',
    );

    var $staging = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'username',
        'password' => 'password',
        'database' => 'stagingDatabaseName',
        'prefix' => '',
    );

    //var $dev = ...

    // Chooses production or staging depending on URL
    function __construct ()
    {
        if(isset($_SERVER['SERVER_NAME']))
        {
            switch($_SERVER['SERVER_NAME'])
            {
                case 'myhostname.com':
                case 'www.myhostname.com':
                    $this->default = $this->prod;
                    break;
                case 'staging.myhostname.com':
                    $this->default = $this->staging;
                    break;
                default:
                    $this->default = $this->dev;
            }
        }
        else // Use local for any other purpose
        {
            $this->default = $this->dev;
        }
    }
}
0
source

Source: https://habr.com/ru/post/1710338/


All Articles