1

这个问题与在 PhalconPHP 中附加多个配置数组有关

我正在尝试使用 get 方法从 DI 中检索对象。

对象是这样设置的

// $new_array the array with the merged data. Load it in a 
// \Phalcon\Config object
$config = new \Phalcon\Config($new_array);

//Store the config in your DI container for easier use
$di->set('config', $config);

这是我打电话时收到的错误消息

$new_array = $di->get('config');

[未捕获的异常 'Phalcon\DI\Exception' 带有消息'无效的服务定义。缺少'className'参数']

我已经坚持了几天了,所以非常感谢我能得到的任何帮助。

4

3 回答 3

2

在集合中尝试这个:

$di->set('config', function() {
   ...
   return new \Phalcon\Config($new_array);
});
于 2013-12-23T06:06:14.713 回答
0

看起来你正在做$di->set('config', $new_array);而不是$di->set('config', $config);:)

于 2012-11-30T16:12:07.547 回答
0

如果您的 $config 变量是一个数组。您可以在Missing 'className' 参数处参考我的答案

这是转贴:我发现 Phalcon DI 容器使用数组进行构造函数注入。因此,如果您将数组设置到 Phalcon DI 容器中,它会理解您要使用构造函数注入来设置对象,并且需要“className”定义。您可以在https://docs.phalconphp.com/3.4/en/di的构造函数注入部分检查这一点。

文档中的构造函数注入示例:

$di->set(
    'response',
    [
        'className' => 'Phalcon\Http\Response'
    ]
);

$di->set(
    'someComponent',
    [
        'className' => 'SomeApp\SomeComponent',
        'arguments' => [
            [
                'type' => 'service',
                'name' => 'response',
            ],
            [
                'type'  => 'parameter',
                'value' => true,
            ],
        ]
    ]
);

我的解决方案:

  1. 假设我想将此配置数组 ['key' => 'value'] 设置为 DI。
  2. 我创建了 MyConfigFactory 类,它具有返回 ['key' => 'value'] 的函数构建。
  3. 我注入我的配置如下:

    $di->set('myConfigFactory', new MyConfigFactory());
    $di->set('config', function () use ($di) {
        return $di->get('myConfigFactory')->build();
    });
    
于 2019-04-27T14:18:56.277 回答