0

您能否给我一个提示,为什么PHP-DI 与 Zend Framework 2的集成对我不起作用(使用 Apache/2.4.9 (Win64) PHP/5.5.12 和 Apache/2.2.22 (Win32) PHP/5.3.13 复制) )。

作曲家.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.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(
        ...
    ),
);

配置/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

您的任何想法将不胜感激。

4

3 回答 3

1

它不起作用,因为您使用的是旧的 YAML 语法,但是从 PHP-DI v4.0 开始,语法现在是 PHP

前往文档了解语法: http: //php-di.org/doc/definition.html

于 2015-02-25T04:15:19.040 回答
0

对于服务:

出厂配置:

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

工厂类:

class MyService implements FactoryInterface
{

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

对于控制器:

控制器工厂.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;
    }
}

类 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'
        ),
于 2015-02-23T15:58:37.377 回答
0

我遵循 Purple Hexagon 给出的建议,这是使用服务管理器的工作实现:

模块/形态/配置

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

模块/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);
    }
}

为什么我不喜欢这个:

  1. 我必须使用“虚拟”代码来获取 serviceManager。那是“虚拟的”,因为这根本与我的应用程序的业务逻辑无关。
  2. ServiceManager 提供的依赖注入方法使我的代码依赖于 ServiceManager 本身。通常我不应该关心 bean/对象是如何注入的,因此不应该在我的代码中引用任何类型的容器或 ServiceManager。

我认为PHP-DI更接近 Java Spring 使用的 bean 注入模型(我认为这很好)。不幸的是,它仍然不适合我。最后,从容器中获取对象的方法也适用于 PHP-DI。

于 2015-02-23T15:54:52.940 回答