PHP-DI无法与ZendFramework2集成

时间:2015-02-23 13:00:29

标签: php zend-framework zend-framework2 php-di

请你提示为什么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",
       ...
},
    ...

设置\ application.config.php

<?php
return array(
    'modules' => array(
        'Morpho',
        'DI\ZendFramework2',
    ),
    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);
?>

模块/形态/ 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(
        ...
    ),
);

配置/ di.yml

Morpho\Service\PartOfSpeechService:
    class: Morpho\Service\PhpMorphyPartOfSpeechService

模块/形态/ SRC /形态/控制器/ 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

任何你的想法都会非常感激。

3 个答案:

答案 0 :(得分:1)

它不起作用,因为您使用旧的YAML语法,但是since PHP-DI v4.0 the syntax is now PHP

转到文档以了解语法:http://php-di.org/doc/definition.html

答案 1 :(得分:0)

我遵循了Purple Hexagon提出的建议,这是使用Service Manager的工作实现:

<强>模块/形态/配置

...
'service_manager' => array(
    'services' => array(
        "PartOfSpeechService" => new Morpho\Service\PhpMorphyPartOfSpeechService(),
    ),
),
...

<强>模块/形态/ SRC /形态/控制器/ PartOfSpeechController.php

class PartOfSpeechController extends AbstractRestfulController {
    ...
    public function processPostData(Request $request) {
        $serviceManager = $this->getServiceLocator();
        $partsOfSpeech = $serviceManager->get("PartOfSpeechService")->getPartsOfSpeech($request->getPost("phrase"),
                $request->getPost("language"));
        return new JsonModel($partsOfSpeech);
    }
}

为什么我不喜欢这个:

  1. 我必须使用“虚拟”代码来获取serviceManager。这是“假的”,因为这与我的应用程序的业务逻辑无关。
  2. ServiceManager提供的依赖注入方法使我的代码依赖于ServiceManager本身。通常我不应该关心如何注入bean /对象,因此不应该在我的代码中引用任何类型的容器或ServiceManager。
  3. 我认为PHP-DI更接近Java Spring使用的bean注入模型(我相信这很好)。不幸的是,它仍然不适合我。 最后,从容器中获取对象的方法也在PHP-DI中工作。

答案 2 :(得分:0)

对于服务:

工厂配置:

        'factories' => array(
            'MyService' => 'Application\Factory\MyService',
        ),

工厂类:

class MyService implements FactoryInterface
{

    public function createService(ServiceLocatorInterface $serviceManager)
    {
        $purifier = new MyService($serviceManager->get('MyAwesomeDependency'));
        return $purifier;
    }
}

对于控制器:

ControllerFactory.php:

class PartOfSpeechControllerFactory
{
    public function __invoke($serviceLocator)
    {
        // Service locator here is the ControllerManager so get ServiceManager 
        $serviceManager = $serviceLocator->getServiceLocator();

        $controller = new PartOfSpeechController($serviceManager->get('PartOfSpeechService'));

        return $controller;
    }
}

class PartOfSpeechController.php

class PartOfSpeechController extends AbstractRestfulController {


    protected $partOfSpeechService;

    public function __construct(PartOfSpeechService $partOfSpeechService)
    {
         $this->partOfSpeechService = $partOfSpeechService;
    }


    public function processPostData(Request $request) {
         $var = $this->partOfSpeechService->serviceMethod();   
    }
}

控制器的配置:

'factories' => array(
                'Application\Controller\PartOfSpeechController' => 'Application\Factory\PartOfSpeechControllerFactory'
        ),