문제

I have a site running in localhost as a development environment and in a server for production.

There are some differences in some configuration files between both and I every time I have to update the changes in the server I have to be careful not to overwrite some files which are different.

I would like to be able to simplify this process by creating just one single file with the correct configuration for each environment.

I need to read that file currently in this Config files:

  • app/Config/email.php
  • app/Config/routes.php

And ideally, if possible in:

  • app/Vendor/Vendor_name/vendor_file.php

Is it possible somehow?

I have tried to use Configure::read and Configure::write but it seems it can not be done inside email settings such as public $smtp or in the routes file.

Thaks.

도움이 되었습니까?

해결책

The routes file is simply a php file with calls to the router. You could very simply split it up into multiple files and load them yourself:

app/Config/
  routes.php
  routes_dev.php
  routes_production.php

routes.php would then load the proper routes file.

<?php
if ($env == 'dev') {
  include 'routes_dev.php';
} else {
  include 'routes_production.php';
}

The email config is also just a php file. You could write a function to set the proper default config based on the environment.

class EmailConfig {

  public function __construct() {
    if ($env == 'dev') {
      $this->default = $this->dev;
    }
  }

  public $default = array(
      'host' => 'mail.example.com',
      'transport' => 'Smtp'
  );

  public $dev = array(
      'host' => 'mail2.example.com',
      'transport' => 'Smtp'
  );

}

As for vendor files, that's a case by case basis.

If you have a deployment system, it might be better to actually have separate files for each environment (maybe even a full config directory) and rename them after the deployment build is complete, making Cake and your code none the wiser.

다른 팁

The way I used to handle this situation was to add environment variables to the apache virtualhost configuration.

SetEnv cake_apps_path /var/www/apps/
SetEnv cake_libs_path /var/www/libs/

This allowed me to then pull $_SERVER['cake_apps_path'] and $_SERVER['cake_libs_path']. Then each developer can set his own variables in his own virtualhost config, and you add that to the server's virtualhost config, and you're done. Each developer can have their own pathing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top