PHP-DI / ZF2-Bridge

PHP-DI integration with Zend Framework 2
MIT License
11 stars 14 forks source link

Cannot make PHP_DI is working with Zend2 #7

Closed FiMka closed 9 years ago

FiMka commented 9 years ago

Hi Matthieu,

Could you please give a hint why PHP-DI can be not working for me in integration with Zend2 (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",
        ...
    },
    "autoload": {
        "classmap": [
            ...
        ],
        "psr-0": {
            "Morpho": "module/Morpho/src",
            "phpMorphy": "vendor/makhov/phpmorphy/src/common.php"
        }
    }
}

module/Morpho/config.module.config.php:

<?php
return array(
    'modules' => array(
        'DI\ZendFramework2',
    ),
    'router' => array(
        ...
        ),
    ),
    'controllers' => array(
        ...
    ),
    'view_manager' => array(
        ...
    ),
    'service_manager' => array(
        'factories' => array(
            'DI\Container' => function() {
                $builder = new DI\ContainerBuilder();
                $builder->addDefinitionsFromFile("config/di.yml");
                return $builder->build();
            },
        ),
    ),
);

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:  Call to a member function getPartsOfSpeech() on a
non-object in \module\Morpho\src\Morpho\Controller\PartOfSpeechController.php on line 34

The injection is working normally if obtain the controller object using container but not under Zend2 MVC. Any your thoughts would be really appreciated.

FiMka commented 9 years ago

It is interesting if I change the controller to:

class PartOfSpeechController extends AbstractRestfulController {
    ...
    public $partOfSpeechService;

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

    public function processPostData(Request $request) {
        $partsOfSpeech = $this->partOfSpeechService->getPartsOfSpeech("test", "en_EN");
        return new JsonModel($partsOfSpeech);
    }

I get:

PHP Catchable fatal error:  Argument 1 passed to
Morpho\Controller\PartOfSpeechController::__construct()
must be an instance of
Morpho\Service\PartOfSpeechService, none given, called in
\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php
on line 180 and defined in \module\Morpho\src\Morpho\Controller\PartOfSpeechController.php on
line 26
mnapoli commented 9 years ago

Hello, sorry for the late response I was away in holidays and I'm slowly going through my mails…

You are using a YAML config file which is not supported since PHP-DI 4.0! So this doesn't work.

Have a look at the "PHP configuration" section in the documentation: http://php-di.org/doc/definition.html

mnapoli commented 9 years ago

In your example the config file would be something like that:

(config.php)

<?php

return [
    'Morpho\Service\PartOfSpeechService' => DI\object('Morpho\Service\PhpMorphyPartOfSpeechService'),
];
mnapoli commented 9 years ago

See also this blog post if you are interested to know the details: http://php-di.org/news/06-php-di-4-0-new-definitions.html

FiMka commented 9 years ago

Hi Matthieu, thank you very much for support and explanation!