简体   繁体   English

PHP-DI无法与ZendFramework2集成

[英]PHP-DI is not working in integration with ZendFramework2

Could you please give a hint why PHP-DI integration with Zend Framework 2 is not working for me (reproduced with Apache/2.4.9 (Win64) PHP/5.5.12 and Apache/2.2.22 (Win32) PHP/5.3.13). 您能否提示为什么Zend Framework 2的PHP-DI集成对我不起作用(由Apache / 2.4.9(Win64)PHP / 5.5.12和Apache / 2.2.22(Win32)PHP / 5.3.13复制) )。

composer.json : 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 : config \\ application.config.php

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

module/Morpho/config.module.config.php : module / 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 : config / di.yml

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

module/Morpho/src/Morpho/Controller/PartOfSpeechController : 模块/ 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);
    }
}

When running this code under apache each time I get: 每次在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

Any your thoughts would be really appreciated. 您的任何想法都将不胜感激。

It doesn't work because you are using the old YAML syntax, but since PHP-DI v4.0 the syntax is now PHP . 它不起作用,因为您使用的是旧的YAML语法,但是自PHP-DI v4.0起,该语法现在为PHP

Head over to the documentation to learn about the syntax: http://php-di.org/doc/definition.html 转至文档以了解语法: http : //php-di.org/doc/definition.html

I followed the suggestion given by Purple Hexagon and here is a working implementation using Service Manager: 我遵循了Purple Hexagon的建议,这是使用Service Manager的有效实施:

module/Morpho/config : 模块/ Morpho / config

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

module/Morpho/src/Morpho/Controller/PartOfSpeechController.php : module / Morpho / src / Morpho / Controller / 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);
    }
}

Why I don't like this: 为什么我不喜欢这样:

  1. I have to use a "dummy" code to obtain serviceManager. 我必须使用“虚拟”代码来获取serviceManager。 That's "dummy" because that's not related to business logic of my application at all. 那是“虚拟的”,因为那根本与我的应用程序的业务逻辑无关。
  2. The Dependency Injection approach provided by ServiceManager makes my code dependent on the ServiceManager itself. ServiceManager提供的依赖关系注入方法使我的代码依赖于ServiceManager本身。 Typically I should not care how a bean/object is injected and so shouldn't refer to any kind of container or ServiceManager in my code. 通常,我不应该在乎如何注入bean /对象,因此在我的代码中也不应引用任何类型的容器或ServiceManager。

I think PHP-DI is much more closer to the bean injection model used by Java Spring (that I believe is good). 我认为PHP-DI更接近Java Spring使用的bean注入模型(我认为很好)。 Unfortunately it is still not working for me. 不幸的是,它仍然对我不起作用。 And finally, the approach of obtaining object from container was working in PHP-DI as well. 最后,从容器获取对象的方法也适用于PHP-DI。

For a service: 对于服务:

factory config: 出厂配置:

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

Factory class: 工厂类别:

class MyService implements FactoryInterface
{

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

For a controller: 对于控制器:

ControllerFactory.php: 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 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();   
    }
}

The config for controller: 控制器的配置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM