【发布时间】:2015-02-23 13:00:29
【问题描述】:
能否请您提示为什么PHP-DI integration with Zend Framework 2 对我不起作用(使用 Apache/2.4.9 (Win64) PHP/5.5.12 和 Apache/2.2.22 (Win32) PHP/5.3.13 转载)。
composer.json:
{
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.3.5",
"mnapoli/php-di": "4.4.6",
"mnapoli/php-di-zf2": "0.3.0",
...
},
...
config\application.config.php:
<?php
return array(
'modules' => array(
'Morpho',
'DI\ZendFramework2',
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
),
);
?>
模块/Morpho/config.module.config.php:
<?php
return array(
'service_manager' => array(
'factories' => array(
'DI\Container' => function() {
$builder = new DI\ContainerBuilder();
$builder->addDefinitionsFromFile("config/di.yml");
return $builder->build();
},
),
),
'router' => array(
...
),
'controllers' => array(
...
),
'view_manager' => array(
...
),
);
config/di.yml:
Morpho\Service\PartOfSpeechService:
class: Morpho\Service\PhpMorphyPartOfSpeechService
模块/Morpho/src/Morpho/Controller/PartOfSpeechController:
class PartOfSpeechController extends AbstractRestfulController {
...
/**
* @Inject
* @var PartOfSpeechService
*/
public $partOfSpeechService;
public function processPostData(Request $request) {
$partsOfSpeech = $this->partOfSpeechService->getPartsOfSpeech("test", "en_EN");
return new JsonModel($partsOfSpeech);
}
}
当我每次在apache下运行这段代码时:
PHP Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException'
with message 'Module (DI\ZendFramework2) could not be initialized.' in \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php:195
Stack trace:
0 \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(169): Zend\ModuleManager\ModuleManager->loadModuleByName(Object(Zend\ModuleManager\ModuleEvent))
1 \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(96): Zend\ModuleManager\ModuleManager->loadModule('DI\ZendFramewor...')
2 [internal function]: Zend\ModuleManager\ModuleManager->onLoadModules(Object(Zend\ModuleManager\ModuleEvent))
3 \vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468):
call_user_func(Array, Object(Zend\ModuleManager\ModuleEvent))
4 \vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventM in \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php on line 195
您的任何想法将不胜感激。
【问题讨论】:
-
为什么不直接使用 zf2 服务管理器呢? framework.zend.com/manual/current/en/modules/…
-
这是一个很好的问题。老实说,我是 ZendFramework 的新手。我最近才发现SM。对我来说,PHP-DI(带有注释)使用的 DI 方法看起来更接近 Spring @Inject。这就是我坚持下去的原因。我会尝试用 SM 重写并在这里返回结果。
标签: php zend-framework zend-framework2 php-di