【问题标题】:Appending multiple config arrays in PhalconPHP在 PhalconPHP 中附加多个配置数组
【发布时间】:2023-03-03 04:40:01
【问题描述】:

目前,我正在引导程序中加载多个包含 PHP 原生数组的配置文件。

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

通过此设置,“config-other.php”将覆盖“config-global.php”的 $settings 数组。

能否请我就在我的引导程序中附加数组的最佳方式获得一些建议。

提姆

更新

这是我尝试实施 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 在 /public/frontend/index.php 上 第 127 行

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

【问题讨论】:

  • 这是期望的行为吗?即 config-global.php 包含假设 $settings 作为数组。然后 config-other.php 还有另一个 $settings 数组。后者将覆盖前者。您希望它们合并吗?
  • 嗨 Nikolaos,是的,我需要来自多个文件的合并 $settings 数组。这背后的原因是我的应用程序实际上是很多应用程序,每个应用程序都有自己的数据库连接详细信息。我希望这是有道理的

标签: php phalcon


【解决方案1】:

试试这个:

在 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 容器引用、始终加载基本数组(全局)等。

EDIT:在 cmets 后稍作修改

【讨论】:

  • 嗨 Nikolaos,非常感谢。这是一种享受!一个快速的相关问题。我在我的 processConfig 函数调用下定义了一个 $config 常量,“$config = new \Phalcon\Config($new_config);”。这当然不起作用,因为它超出了 $new_config 存在的函数范围。我知道将 $new_config 设为全局变量是个坏主意,但是您对我如何定义新的配置变量有什么建议吗?我也尝试将调用添加到您的 processConfig 函数,但它不起作用。如您所知,我对 OOP 原则还是很陌生。
  • 你不需要经历那个麻烦。使用 DI 容器。检查我上面的编辑。
  • 您好 Nikolaos,谢谢您的回答!我确实尝试过使用 DI,但我无法让它工作。当我调用以下行时,我得到以下信息。 “$new_array = $di->get('config_array');” [未捕获的异常 'Phalcon\DI\Exception' 带有消息'无效的服务定义。缺少'className'参数']。我已经尝试过 $di->get 上的文档,但我不明白。很抱歉让你这么痛苦。 docs.phalconphp.com/en/0.5.0/api/Phalcon_DI.html#methods
  • 由于这个问题与这个问题没有直接关系,我在stackoverflow.com/questions/13636858/…提出了一个新问题
  • @Tim 在阅读了您的其他问题后,我对代码进行了一些额外的更改。请看一下,让我知道这是否适合您。
猜你喜欢
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2018-09-28
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 2021-11-17
相关资源
最近更新 更多