This library provides integration for PHP-DI with Zend Framework.
PHP-DI is a Dependency Injection Container for PHP.
If you are looking for Zend Framework 2 integration, head over here.
Require the libraries with Composer:
{
"require": {
"php-di/php-di": "*",
"php-di/zend-framework-bridge": "*"
}
}
To use PHP-DI in your Zend Framework application, you need to edit application_root/config/application.config.php
:
// ...
'modules' => [
'DI\ZendFramework',
'Zend\Router',
'Zend\Mvc\Console',
...
],
'service_manager' => [
// ...
'factories' => [
'DI\Container' => DI\ZendFramewor\Service\DIContainerFactory::class,
],
],
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.
To configure the module, you have to override the module config somewhere at config/autoload/global.php or config/autoload/local.php.
return [
'phpdi-zf' => [
...
]
];
return [
'phpdi-zf' => [
'definitionsFile' => realpath(__DIR__ . '/../my.custom.def.config.php'),
]
];
return [
'phpdi-zf' => [
'useAnntotations' => true,
]
];
return [
'phpdi-zf' => [
'cache' => [
'adapter' => 'filesystem',
'namespace' => 'your_di_cache_key',
'directory' => 'your_cache_directory', // default value is data/php-di/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-zf' => [
'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
],
]
];
If you're using Memcached, you should have only one project per memcached instance.
return [
'phpdi-zf' => [
'cache' => [
'adapter' => 'memcached',
'host' => 'localhost', // default is localhost
'port' => 11211, // default is 11211
],
]
];
To clear the definition cache, run the following command from the project root:
php public/index.php php-di-clear-cache
Run composer install --dev
After that, open terminal and go to the quickstart
folder and run php -S 0.0.0.0:8080 -t public public/index.php
Open a browser and go to this address: http://localhost:8080/hello
You should see a page with a greeting coming from the GreetingController, which is being resolved by the PHP-DI configuration.