Missing 'className' parameter

Deadly 提交于 2020-01-17 06:22:03

问题


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.

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']);

I am getting the below error.

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}

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
)

回答1:


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,
            ],
        ]
    ]
);

MY SOLUTION:

  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();
    });
    

Good luck.



来源:https://stackoverflow.com/questions/39202801/missing-classname-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!