2

cloudControl (heroku のような PaaS プロバイダー) コンテナーで Symfony2 と MongoDB を使用したいと考えています。Symfony2は MongoDB の使用をサポートするようになりました:

# app/config/config.yml
doctrine_mongodb:
    connections:
        default:
            server: mongodb://localhost:27017
            options: {}
    default_database: test_database
    document_managers:
        default:
            auto_mapping: true

また、MongoDB は PaaS アドオンであるため、静的な接続資格情報はありません。それらはコンテナによって生成されます。cloudControl は、PHP で資格情報にアクセスするために次の方法を提供します。

$credfile = file_get_contents($_ENV['CRED_FILE'], false);
$credentials = json_decode($credfile, true);
$uri = $credentials["MONGOLAB"]["MONGOLAB_URI"];
$m = new Mongo($uri);
$db = $m->selectDB(myDbName);
$col = new MongoCollection($db, myCollection);

これらの動的に取得された資格情報を Symfony2 に取得するにはどうすればよいconfig.ymlですか?

4

1 に答える 1

2

解決策は、 Symfony2 Miscellaneous Configurationを使用することです。

したがって、app/config/credentials.php以下の内容でファイルを作成します。

<?php
if (isset($_ENV['CRED_FILE'])) {

    // read the credentials file
    $string = file_get_contents($_ENV['CRED_FILE'], false);
    if ($string == false) {
        throw new Exception('Could not read credentials file');
    }

    // the file contains a JSON string, decode it and return an associative array
    $creds = json_decode($string, true);

    // overwrite config server param with mongolab uri
    $uri = $creds["MONGOLAB"]["MONGOLAB_URI"];
    $container->setParameter('doctrine_mongodb.connections.default.server', $uri);
}

次に、app/config/config.yml追加で:

imports:
    - { resource: credentials.php }

これで問題が解決するかどうかお知らせください。

于 2014-06-18T09:38:50.673 に答える