File: zf2.md

package info (click to toggle)
php-di 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,932 kB
  • sloc: php: 10,572; makefile: 42; xml: 17; sh: 10; pascal: 5
file content (124 lines) | stat: -rw-r--r-- 2,509 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
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`:

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

That's it!

## Usage

Now you can inject dependencies in your controllers.

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

```php
class GuestbookController extends AbstractActionController
{
    /**
     * This dependency will be injected by PHP-DI
     */
    #[Inject]
    private \Application\Service\GuestbookService $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](../php-definitions.html) 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`:

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

### Override definitions file location

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

### Enable file cache

```php
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

```php
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](https://github.com/PHP-DI/ZF2-Bridge).