简体   繁体   中英

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).

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',
        ),
    ),
);
?>

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 :

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

module/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:

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 .

Head over to the documentation to learn about the syntax: http://php-di.org/doc/definition.html

I followed the suggestion given by Purple Hexagon and here is a working implementation using Service Manager:

module/Morpho/config :

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

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. 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. 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.

I think PHP-DI is much more closer to the bean injection model used by Java Spring (that I believe is good). Unfortunately it is still not working for me. And finally, the approach of obtaining object from container was working in PHP-DI as well.

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:

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

The config for controller:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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