在PhalconPHP中添加多个配置数组

时间:2012-11-21 19:38:51

标签: php phalcon

目前我正在我的bootstrap中加载包含PHP本机数组的多个配置文件。

require "app/configuration/config-global.php";
require "app/configuration/config-other.php";

使用此设置" config-other.php"覆盖" config-global.php"。

的$ settings数组

请给我一些关于在我的引导程序中附加数组的最佳方法的建议。

更新

以下是我的bootstap文件设置的缩减版本,试图实施Nikolaos的建议。

class Application extends \Phalcon\Mvc\Application
{

    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    public function _registerServices()
    {

        //Define constants
        $di = new \Phalcon\DI\FactoryDefault();

        $loader = new \Phalcon\Loader();

        $di->set('registry', function () {
            return new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
        });

        //load our config into the registry
        //$di->set('config', $config);

        $this->setDI($di);
    }

    public function _loadConfig()
    {

        $di = \Phalcon\DI::getDefault();

        $this->processConfig('appConfig1');
        $this->processConfig('globalConfig');

        // Remember config_array is the merged array in the DI container
        $new_array = $di->registry->offsetGet('config_array');

        // Optional, store the config in your DI container for easier use
        $di->set('config', function () use ($new_array) {
                return new \Phalcon\Config($config);
            }
        );

    }

    public function main()
    {

        $this->_registerServices();
        $this->_loadConfig();

        echo $this->handle()->getContent();
    }

    public function processConfig($name)
    {

    $config = array();
    $di     = \Phalcon\DI::getDefault();

    if ($di->registry->offsetExists('config_array'))
    {
        $config = $di->registry->offsetGet('config_array');
    }

    // Now get the file from the config
    require "/config/{$name}.php";

    // $settings came from the previous require
    $new_config = array_merge($config, $settings);

    // Store it in the DI container
    $di->registry->offsetSet('config_array', $new_config);

    }

}

$application = new Application();
$application->main();

使用上面的配置我得到:

  

[02-Dec-2012 09:10:43] PHP注意:未定义的属性:   Phalcon \ DI \ FactoryDe​​fault :: $ registry in /public/frontend/index.php on   第127行

     

[02-Dec-2012 09:10:43] PHP致命错误:致电会员   函数offsetExists()在/public/frontend/index.php中的非对象上   在第127行

1 个答案:

答案 0 :(得分:4)

试试这个:

在DI容器中注册新服务

// Get the DI container
$di = \Phalcon\DI::getDefault();

$di->set(
    'registry', 
    function ()
    {
        return new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
    }
);

以上将是我们将存储配置文件的“注册表”。

public function processConfig($name)
{
    $config = array();
    $di     = \Phalcon\DI::getDefault();

    if ($di->registry->offsetExists('config_array'))
    {
        $config = $di->registry->offsetGet('config_array');
    }

    // Now get the file from the config
    require ROOT_PATH . "app/configuration/{$name}.php";

    // $settings came from the previous require
    $new_config = array_merge($config, $settings);

    // Store it in the DI container
    $di->registry->offsetSet('config_array', $new_config);
}

作为用法,你可以这样做:

processConfig('config-global');
processConfig('config-other');

// Remember config_array is the merged array in the DI container
$di = \Phalcon\DI::getDefault();

// Remember config_array is the merged array in the DI container
$new_array = $di->registry->offsetGet('config_array');

// Optional, store the config in your DI container for easier use
$di->set(
    'config', 
    function () use ($new_array)
    {
        return new \Phalcon\Config($config);
    }
};

容器中的config数组现在具有合并数据。

您还可以创建一个类,该类将封装该功能并在其中具有其他辅助函数以清除DI容器引用,以便始终加载基本数组(全局)等。

编辑:在评论后稍加修改