缺少"className"参数 [英] Missing 'className' parameter

查看:354
本文介绍了缺少"className"参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个旧的项目更改请求,并且该项目是在phalcon 1.2.6版本中开发的.当我尝试执行该应用程序时,该应用程序返回错误.经过一些R& D之后,我发现系统没有从$di对象中找到配置密钥.

I am working on a old project change request and the project was developed in phalcon 1.2.6 verson. When I am trying to execute the application the application returns an error. After doing some R&D I found that the system did not find the config key from the $di object.

当我尝试打印$di对象时,使用键config可以正确打印.但是当尝试访问配置密钥时,我无法访问它.

When I am trying to print the $di object it's printing properly with key config. But when trying to access the config key, I am unable to access it.

系统尝试执行以下代码时,它将引发异常.

When the system tries to execute the below code, it throws an exception.

$di = \Phalcon\DI::getDefault();
print_r($di['config']);

我收到以下错误.

Invalid service definition. Missing 'className' parameter
#0 [internal function]: Phalcon\DI\Service\Builder->build(Object(Phalcon\DI\FactoryDefault), Array, NULL)
#1 [internal function]: Phalcon\DI\Service->resolve(NULL, Object(Phalcon\DI\FactoryDefault))
#2 [internal function]: Phalcon\DI->get('config', NULL)
#3 /var/www/sites/mfs_merged/apps/api/Module.php(44): Phalcon\DI->offsetGet('config')
#4 [internal function]: AppServer\Api\Module->registerServices(Object(Phalcon\DI\FactoryDefault))
#5 /var/www/sites/mfs_merged/public/index.php(64): Phalcon\Mvc\Application->handle()
#6 {main}

下面是我的$di对象的一部分

below is a part of my $di object

Phalcon\DI\FactoryDefault Object
(
    [_services:protected] => Array
        (
            [...] => Phalcon\DI\Service Object
                (....)

            [config] => Phalcon\DI\Service Object
                (
                    [_name:protected] => config
                    [_definition:protected] => Array
                        (
                            [database] => Array
                                (
                                    [adapter] => Oracle
                                    [host] => 172.20.3.228
                                    [username] => XXXXX
                                    [password] => XXXXXXX
                                    [schema] => XE
                                    [dbname] => (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.20.3.228)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))
                                )

                            [memcache] => Array
                                (
                                    [lifetime] => 3600
                                    [host] => localhost
                                    [port] => 11211
                                    [persistent] => 
                                )

                            [soapClient] => Array
                                (
                                    [connectionTimeout] => 60
                                    [exceptions] => 
                                    [trace] => 1
                                    [cache] => 0
                                    [useSoapHeader] => 1
                                    [soapHeader] => Array
                                        (
                                            [username] => XXXXX
                                            [password] => XXXXXX
                                        )
                                )
                            [SMSCodesLogPath] => /var/www/sites/mfs_merged/config/TZ/../../public/files/_SMSTokens/tokens_TZ.log
                        )

                    [_shared:protected] => 1
                    [_sharedInstance:protected] => 
                )
        )

    [_sharedInstances:protected] => Array
        (.....)

    [_freshInstance:protected] => 1
)

推荐答案

我也遇到了同样的问题.我发现Phalcon DI容器使用数组进行构造函数注入.因此,如果将数组设置到Phalcon DI容器中,它将理解要使用构造函数注入来设置对象,并且需要"className"定义.您可以在 https://docs.phalconphp.com/3.4/en的构造函数注入"部分中进行检查/di .

I faced the same issue with you. and I found that Phalcon DI container use array for Constructor Injection. 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. You can check this at Constructor Injection section at 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,
            ],
        ]
    ]
);

我的解决方案:

  1. 假设我要将这个配置数组['key'=>'value']设置为DI.
  2. 我创建MyConfigFactory类,该类具有函数构建以返回['key'=>'value'].
  3. 我按如下所示注入配置:

  1. Suppose that I want to set this config array ['key' => 'value'] into DI.
  2. I create MyConfigFactory class, which has function build to return ['key' => 'value'].
  3. I inject my config as below:

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

祝你好运.

这篇关于缺少"className"参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆