我正在处理旧的项目变更请求,该项目是在phalcon 1.2.6版本中开发的。当我尝试执行应用程序时,应用程序返回错误。在做了一些R& D之后,我发现系统没有从$di
对象中找到配置密钥。
当我尝试打印$di
对象时,它使用键配置正确打印。但是在尝试访问配置密钥时,我无法访问它。
当系统尝试执行以下代码时,它会抛出异常。
$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
对象
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
)
答案 0 :(得分:0)
我也遇到了同样的问题。我发现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,
],
]
]
);
我的解决方案:
我按如下所示注入配置:
$di->set('myConfigFactory', new MyConfigFactory());
$di->set('config', function () use ($di) {
return $di->get('myConfigFactory')->build();
});
祝你好运。