5.4. Zend Framework 2

未匹配的标注

PHP-DI in Zend Framework 2

If you are using Zend Framework 2, PHP-DI provides an easy and clean integration with the existing framework's container.

Set up

First, install the bridge:

composer require php-di/zf2-bridge

Register it in application_root/config/application.config.php:

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

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

That's it!

If you want to use annotations, please read the "Configuration" section below.

Usage

Now you can inject dependencies in your controllers.

Here is an example of the GuestbookController of the quickstart (using annotations):

class GuestbookController extends AbstractActionController
{
    /**
     * This dependency will be injected by PHP-DI
     * @Inject
     * @var \Application\Service\GuestbookService
     */
    private $guestbookService;

    public function indexAction()
    {
        $this->view->entries = $this->guestbookService->getAllEntries();
    }
}

If you'd like to define injections using a configuration file, put them in application_root/config/php-di.config.php:

<?php
return [
    'Application\Service\GreetingServiceInterface' => DI\create('Application\Service\GreetingService'),
];

Head over to the definitions documentation if needed.

Configuration

To configure PHP-DI itself, you have to override the module config in config/autoload/global.php or config/autoload/local.php:

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

Enable or disable annotations

Annotations are disabled by default since PHP-DI 5. To enable them, use the following config:

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

Override definitions file location

return [
    'phpdi-zf2' => [
        'definitionsFile' => __DIR__ . '/../my-custom-config-file.php',
    ]
];

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

return [
    'phpdi-zf2' => [
        'cache' => [
            'namespace' => 'your_di_cache_key',
            'adapter' => 'redis',
            'host' => 'localhost', // default is localhost
            'port' => 6379, // default is 6379
        ],
    ]
];

More

Read more on the ZF2-Bridge project on GitHub.


layout: documentation
current_menu: zf2

PHP-DI in Zend Framework 2

If you are using Zend Framework 2, PHP-DI provides an easy and clean integration with the existing framework's container.

Set up

First, install the bridge:

composer require php-di/zf2-bridge

Register it in application_root/config/application.config.php:

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

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

That's it!

If you want to use annotations, please read the "Configuration" section below.

Usage

Now you can inject dependencies in your controllers.

Here is an example of the GuestbookController of the quickstart (using annotations):

class GuestbookController extends AbstractActionController
{
    /**
     * This dependency will be injected by PHP-DI
     * @Inject
     * @var \Application\Service\GuestbookService
     */
    private $guestbookService;

    public function indexAction()
    {
        $this->view->entries = $this->guestbookService->getAllEntries();
    }
}

If you'd like to define injections using a configuration file, put them in application_root/config/php-di.config.php:

<?php
return [
    'Application\Service\GreetingServiceInterface' => DI\create('Application\Service\GreetingService'),
];

Head over to the definitions documentation if needed.

Configuration

To configure PHP-DI itself, you have to override the module config in config/autoload/global.php or config/autoload/local.php:

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

Enable or disable annotations

Annotations are disabled by default since PHP-DI 5. To enable them, use the following config:

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

Override definitions file location

return [
    'phpdi-zf2' => [
        'definitionsFile' => __DIR__ . '/../my-custom-config-file.php',
    ]
];

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

return [
    'phpdi-zf2' => [
        'cache' => [
            'namespace' => 'your_di_cache_key',
            'adapter' => 'redis',
            'host' => 'localhost', // default is localhost
            'port' => 6379, // default is 6379
        ],
    ]
];

More

Read more on the ZF2-Bridge project on GitHub.

(这段原文并没有,无须翻译)

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/php-di/6.0/fram...

译文地址:https://learnku.com/docs/php-di/6.0/fram...

上一篇 下一篇
贡献者:1
讨论数量: 0
发起讨论 只看当前版本


暂无话题~