Skip to content

Commit 7dd2386

Browse files
LordSimalADmad
andauthored
add more doc for component DI (#7922)
* add more doc for component DI * Update en/development/dependency-injection.rst Co-authored-by: ADmad <[email protected]> --------- Co-authored-by: ADmad <[email protected]>
1 parent e3526dd commit 7dd2386

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

en/controllers/components.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,8 @@ in your controller, you could access it like so::
147147
properties they share the same 'namespace'. Be sure to not give a
148148
component and a model the same name.
149149

150-
.. warning::
151-
152-
Component methods **don't** have access to :doc:`/development/dependency-injection`
153-
like Controller actions have. Use a service class inside your controller actions
154-
instead of a component if you need this functionality.
150+
.. versionchanged:: 5.1.0
151+
Components are able to use :doc:`/development/dependency-injection` to receive services.
155152

156153
.. _creating-a-component:
157154

en/development/dependency-injection.rst

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ CakePHP will use the :term:`DI container` in the following situations:
1414

1515
* Constructing controllers.
1616
* Calling actions on your controllers.
17+
* Constructing Components.
1718
* Constructing Console Commands.
1819
* Constructing Middleware by classname.
1920

20-
A short example would be::
21+
Controller Example
22+
==================
23+
24+
::
2125

2226
// In src/Controller/UsersController.php
2327
class UsersController extends AppController
@@ -45,7 +49,10 @@ database. Because this service is injected into our controller, we can easily
4549
swap the implementation out with a mock object or a dummy sub-class when
4650
testing.
4751

48-
Here is an example of an injected service inside a command::
52+
Command Example
53+
===============
54+
55+
::
4956

5057
// In src/Command/CheckUsersCommand.php
5158
class CheckUsersCommand extends Command
@@ -76,6 +83,36 @@ a whole to the Container and add the ``UsersService`` as an argument.
7683
With that you can then access that service inside the constructor
7784
of the command.
7885

86+
Component Example
87+
=================
88+
89+
::
90+
91+
// In src/Controller/Component/SearchComponent.php
92+
class SearchComponent extends Command
93+
{
94+
public function __construct(
95+
ComponentRegistry $registry,
96+
private UserService $users
97+
) {
98+
parent::__construct($registry, []);
99+
}
100+
101+
public function something()
102+
{
103+
$valid = $this->users->check('all');
104+
}
105+
}
106+
107+
// In src/Application.php
108+
public function services(ContainerInterface $container): void
109+
{
110+
$container->add(SearchComponent::class)
111+
->addArgument(ComponentRegistry::class)
112+
->addArgument(UsersService::class);
113+
$container->add(UsersService::class);
114+
}
115+
79116
Adding Services
80117
===============
81118

0 commit comments

Comments
 (0)