LoginSignup
1
1

More than 5 years have passed since last update.

PhalconでDBの設定ファイルだけ分離したい!

Last updated at Posted at 2014-02-20

メモレベルで。

どうやって?

プロジェクト構成はsimple。

config.php
<?php
// app/config/config.php

return new \Phalcon\Config(array(
    'application' => array(
        // ...
    ),
));
database.php
<?php
// app/config/database.php

return new \Phalcon\Config(array(
    'database' => array(
        // ...
    ),
));
index.php
<?php
// public/index.php

$config = include __DIR__ . '/../app/config/config.php';
$config->merge(include __DIR__ . '/../app/config/database.php');

// ローダとかDIとか

と、ここまで書いて思ったけど、別に$configにマージしなくてもいいですね。

index.php
<?php
// public/index.php

$config    = include __DIR__ . '/../app/config/config.php';
$db_config = include __DIR__ . '/../app/config/database.php';

// ローダとかDIとか

とかにしておいて、DbAdapterに与えるホスト名とかユーザ名とかを$db_configから取得してやればいいだけですよね。。。

追記

色んなライブラリのソースコードを読んでると、/app/config以下のファイルはこういう名前だ、みたいな決め打ちを結構見かけるので、DBの設定をこんな感じで分離するとうまく動かないかも。
なので、

config.php
<?php
return new \Phalcon\Config(array(
    'application' => array(
        // ...
    ),
    'database' => include dirname(__FILE__) . '/database.php',
));

としておいて、

database.php
<?php
// app/config/database.php

return array(
        // ...
);

merge()しても別によさげ。まあ敢えて\Phalcon\Configにしてやる必要はないかなー、って感じで。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1