2013-04-05 222 views
2

如果我的Yii應用程序有公共Git存儲庫。我想知道如何保持數據庫配置設置爲私人的?我不能忽略整個文件。使用Git管理Yii應用程序

+2

您可以將您的配置分解爲較小的文件。將數據庫配置提取到單獨的文件,並忽略*表示*。 – DCoder 2013-04-05 11:46:59

+0

如果你不需要公共的git倉庫,你總是可以在bitbucket(而不是github)上創建一個私有倉庫,因爲你可以在[bitbucket](https://bitbucket.org/)上擁有無限制的私有倉庫。 – 2013-04-06 04:25:03

回答

0

我不使用Yii所以我不知道它的結構,但我知道的一件事是,你不能忽略git文件的行。所以我更喜歡你gitignore你真正的配置文件,而不是真正的文件,還可以創建一個config_sample文件發佈,並記下的指令得到它的工作進入README.md

7

創建第二忽略配置和合並的結果:

$config=require_once(dirname(__FILE__).'/protected/config/main.php'); 

$configIgnored=require_once(dirname(__FILE__).'/protected/config/ignored.php'); 

require_once($yii); 

$config = CMap::mergeArray($config, $configIgnored); 
Yii::createWebApplication($config)->run(); 
2

首先... 保持在你的倉庫保護/配置/ main.php.dist文件。 'dist'代表'分配'。這個文件是一個普通的配置文件,但沒有明智的數據。例如,您可以擁有:

... 
'db' => array(
    'connectionString' => 'mysql:host=[[hostname]];dbname=[[database]]', 
    'emulatePrepare' => true, 
    'username' => '[[username]]', 
    'password' => '[[password]]', 
    'charset' => 'utf8', 
), 
... 

請記住,您的用戶名,密碼等不是項目內容。這些信息只關注你而不關注你的應用。

第二... 準備您的部署應用程序。例如我使用phing這樣:

<project name="yourProject" default="install"> 
    <target name="install" description="Prepare application for deploy"> 
     <input propertyname="hostname" 
       defaultValue="localhost" promptChar="?">hostname</input> 
     <input propertyname="database" 
       defaultValue="database" promptChar="?">database</input> 
     <input propertyname="username" defaultValue="root" promptChar="?">username</input> 
     <input propertyname="password" defaultValue="root" promptChar="?">password</input> 
     <copy file="protected/config/main.php.dist" 
       tofile="protected/config/main.php" 
       overwrite="false"> 
      <filterchain> 
       <replacetokens begintoken="[[" endtoken="]]"> 
        <token key="username" value="${username}" /> 
        <token key="password" value="${password}" /> 
        <token key="hostname" value="${hostname}" /> 
        <token key="database" value="${database}" /> 
       </replacetokens> 
      </filterchain> 
     </copy> 
    </target> 
</project> 

通過這種方式,你將永遠不會保存在您的存儲庫的配置設置,並只需要運行

$ phing install 

在你的機器配置的應用程序。顯然你需要安裝phing。