简体   繁体   English

使用 DI->Get Phalcon PHP 时出现“无效的服务定义”

[英]"Invalid Service Definition" when using DI->Get Phalcon PHP

This question is related to Appending multiple config arrays in PhalconPHP这个问题与Appending multiple config arrays in PhalconPHP 有关

I am trying to get retrieve an object from the DI using the get method.我正在尝试使用 get 方法从 DI 检索对象。

The object is being set like this对象是这样设置的

// $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);

And this is the error message I am getting when I call这是我打电话时收到的错误信息

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

[Uncaught exception 'Phalcon\\DI\\Exception' with message 'Invalid service definition. [未捕获的异常 'Phalcon\\DI\\Exception' 带有消息 '无效的服务定义。 Missing 'className' parameter']缺少 'className' 参数']

I have been stuck on this for a few days now so would greatly appreciate any help I can get.我已经坚持了几天了,所以非常感谢我能得到的任何帮助。

Try this instead in the set:在集合中试试这个:

$di->set('config', function() {
   ...
   return new \Phalcon\Config($new_array);
});

It looks like you're doing $di->set('config', $new_array);看起来你在做$di->set('config', $new_array); instead of $di->set('config', $config);而不是$di->set('config', $config); :) :)

If your $config variable is an array.如果您的 $config 变量是一个数组。 You can refer my answer at Missing 'className' parameter您可以在Missing 'className' 参数中参考我的答案

Here is the repost: I found that Phalcon DI container use array for Constructor Injection.这是转帖:我发现 Phalcon DI 容器使用数组进行构造函数注入。 So if you set an array into Phalcon DI container, it understands that you want to set an object by using Constructor Injection and it requires "className" definition.因此,如果您将数组设置到 Phalcon DI 容器中,它会理解您要使用构造函数注入来设置对象,并且需要“className”定义。 You can check this at Constructor Injection section at https://docs.phalconphp.com/3.4/en/di .您可以在https://docs.phalconphp.com/3.4/en/di 的构造函数注入部分进行检查。

Example of constructor injection in the document:文档中构造函数注入示例:

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

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

MY SOLUTION:我的解决方案:

  1. Suppose that I want to set this config array ['key' => 'value'] into DI.假设我想将此配置数组 ['key' => 'value'] 设置为 DI。
  2. I create MyConfigFactory class, which has function build to return ['key' => 'value'].我创建了 MyConfigFactory 类,该类具有返回 ['key' => 'value'] 的函数 build。
  3. I inject my config as below:我注入我的配置如下:

     $di->set('myConfigFactory', new MyConfigFactory()); $di->set('config', function () use ($di) { return $di->get('myConfigFactory')->build(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM