Giter Site home page Giter Site logo

zf2-bridge's Introduction

PHP-DI integration with Zend Framework 2

Build Status

This library provides integration for PHP-DI with Zend Framework 2.

PHP-DI is a Dependency Injection Container for PHP.

If you are looking for Zend Framework 1 integration, head over here.

Use

Require the libraries with Composer:

{
    "require": {
        "php-di/php-di": "*",
        "php-di/zf2-bridge": "*"
    }
}

To use PHP-DI in your ZF2 application, you need to edit application_root/config/application.config.php:

    // ...
    'modules' => [
        ...
        'DI\ZendFramework2',
        ...
    ],

    'service_manager' => [
        // ...
        'factories' => [
            'DI\Container' => 'DI\ZendFramework2\Service\DIContainerFactory',
        ],
    ],

That's it!

Now you dependencies are injected in your controllers!

If you'd like to specify the di configuration yourself, create this file: application_root/config/php-di.config.php and save it with your PHP DI configuration e.g. like this:

return [
    'Application\Service\GreetingServiceInterface' => Di\object('Application\Service\GreetingService'),
];

Head over to PHP-DI's documentation if needed.

Fine tuning

To configure the module, you have to override the module config somewhere at config/autoload/global.php or config/autoload/local.php.

return [
    'phpdi-zf2' => [
        ...
    ]
];

Override definitions file location

return [
    'phpdi-zf2' => [
        'definitionsFile' => realpath(__DIR__ . '/../my.custom.def.config.php'),
    ]
];

Enable or disable annotations

return [
    'phpdi-zf2' => [
        'useAnntotations' => true,
    ]
];

Enable APCu

return [
    'phpdi-zf2' => [
        'cache' => [
            'adapter' => 'apcu',
            'namespace' => 'your_di_cache_key',
        ],
    ]
];

Enable file cache

return [
    'phpdi-zf2' => [
        'cache' => [
            'adapter' => 'filesystem',
            'namespace' => 'your_di_cache_key',
            'directory' => 'your_cache_directory', // default value is data/php-di/cache
        ],
    ]
];

Enable redis cache

If you are also using Redis for storing php sessions, it is very useful to configure the php-di cache handler to use a different database, since you might accidentally delete all your sessions when clearing the php-di definitions cache.

return [
    'phpdi-zf2' => [
        'cache' => [
            'namespace' => 'your_di_cache_key',
            'adapter' => 'redis',
            'host' => 'localhost', // default is localhost
            'port' => 6379, // default is 6379
            'database' => 1, // default is the same as phpredis default
        ],
    ]
];

Enable Memcached cache

If you're using Memcached, you should have only one project per memcached instance.

return [
    'phpdi-zf2' => [
        'cache' => [
            'adapter' => 'memcached',
            'host' => 'localhost', // default is localhost
            'port' => 11211, // default is 11211
        ],
    ]
];

Console commands

Clear definition cache

To clear the definition cache, run the following command from the project root:

php public/index.php php-di-clear-cache

zf2-bridge's People

Contributors

andrejvrabec avatar balihoo-jflitton avatar bgaillard avatar carusogabriel avatar mattin avatar mnapoli avatar ocramius avatar rastusik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

zf2-bridge's Issues

ZF3 support

When i try to to use error occured:

Fatal error: Class DI\ZendFramework2\Service\PHPDIAbstractFactory contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Zend\ServiceManager\Factory\AbstractFactoryInterface::canCreate, Zend\ServiceManager\Factory\FactoryInterface::__invoke) in /home/sohorev/workspace/multivpn-php/vendor/php-di/zf2-bridge/src/DI/ZendFramework2/Service/PHPDIAbstractFactory.php on line 23

Why can not I use several projects in one memcached instance?

In README

If you're using Memcached, you should have only one project per memcached instance.

But i can use namespace:

return [
    'phpdi-zf2' => [
        'cache' => [
            'adapter' => 'memcached',
            'host' => 'localhost', // default is localhost
            'port' => 11211, // default is 11211
            'namespace' => 'my_second_project_',
        ],
    ]
];

Cannot make PHP_DI is working with Zend2

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.

ZF2 bridge Bug - Cache factory can not return null

Hi

DI\ZendFramework2\Service\Cache\CacheFactory in createService() method can return null, but ZF2 ServiceLocator does not allow this behaviour - you'll get this :

\Zend\ServiceManager\ServiceManager @ 953
if ($instance === null) {
            throw new Exception\ServiceNotCreatedException('The factory was called but did not return an instance.');
        }

ZF2 version Im using is current stable ("zendframework/zendframework": "~2.5").

Proposed fixes :
Add some 'BlackholeCache' as default service returned via DiCache factory
Or simple, but a little nasty - catch and handle ServiceNotCreatedException

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.