Skip to content

Commit eb8a0ff

Browse files
authored
add docs about built in DI container (#8329)
1 parent 922713d commit eb8a0ff

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

docs/en/development/dependency-injection.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,12 +1327,13 @@ class ArticlesController extends AppController
13271327
> - **Clarity**: Controller focuses on HTTP concerns, service handles business logic
13281328
> - **Maintainability**: Complex workflows are organized into focused classes
13291329
1330-
## Auto Wiring
1330+
## Auto-wiring with league/container
13311331

13321332
> [!WARNING]
13331333
> Auto-wiring is convenient but can impact performance. Enable caching in production environments.
13341334
1335-
Auto Wiring is turned off by default. To enable it:
1335+
When using the default `league/container` implementation, auto-wiring is turned
1336+
off by default. To enable it:
13361337

13371338
```php
13381339
// In src/Application.php
@@ -1360,3 +1361,55 @@ $container->delegate(
13601361
```
13611362

13621363
Read more about auto wiring in the [PHP League Container documentation](https://container.thephpleague.com/4.x/auto-wiring/).
1364+
1365+
## Built-in Container
1366+
1367+
::: info Added in version 5.4.0
1368+
:::
1369+
1370+
CakePHP includes its own PSR-11-compatible dependency injection container in
1371+
the `Cake\Container` namespace. The `league/container` implementation remains
1372+
the default so that existing applications continue to work unchanged. To use
1373+
the built-in container, set `App.container` to `cake` in `config/app.php`:
1374+
1375+
```php
1376+
return [
1377+
'App' => [
1378+
'container' => 'cake',
1379+
],
1380+
];
1381+
```
1382+
1383+
CakePHP exposes the built-in container through a compatibility bridge. Continue
1384+
to type-hint `Cake\Core\ContainerInterface` in your application's `services()`
1385+
method; service definitions using `add()`, `addShared()`, `extend()`, tags, and
1386+
delegates work with either implementation.
1387+
1388+
### Auto-wiring
1389+
1390+
Unlike the default `league/container` implementation, the built-in container
1391+
enables auto-wiring and caches auto-wired resolutions by default. It can create
1392+
concrete classes and resolve their class-typed constructor dependencies without
1393+
an explicit service definition:
1394+
1395+
```php
1396+
class OrderService
1397+
{
1398+
public function __construct(private PaymentGateway $gateway)
1399+
{
1400+
}
1401+
}
1402+
1403+
// Only the interface needs an explicit mapping. OrderService is auto-wired.
1404+
$container->add(PaymentGateway::class, StripePaymentGateway::class);
1405+
```
1406+
1407+
Define services explicitly when a constructor needs an interface, a scalar or
1408+
array value, or another value that cannot be determined from its type. Explicit
1409+
definitions also let you configure constructor arguments and control whether a
1410+
service is shared.
1411+
1412+
When using the built-in container, do not add
1413+
`League\Container\ReflectionContainer`; auto-wiring is already enabled. The
1414+
following [auto-wiring instructions](#auto-wiring-with-leaguecontainer) apply
1415+
only when using the default `league/container` implementation.

0 commit comments

Comments
 (0)