Skip to content

Commit 68d52e1

Browse files
authored
Merge pull request #7883 from cakephp/components-dic
Add documentation for DI + components
2 parents 2dccf8f + 729fe29 commit 68d52e1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

en/appendices/5-1-migration-guide.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ Core
7676
a typesafe way to cast request data or other input and return ``null`` when conversion fails.
7777
- ``pathCombine()`` was added to help build paths without worrying about duplicate and trailing slashes.
7878

79+
Controller
80+
----------
81+
82+
- Components can now use the DI container to have dependencies resolved and
83+
provided as constructor parameters just like Controllers and Commands do.
84+
7985
Database
8086
--------
8187

en/controllers/components.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@ component would look something like this::
183183
All components must extend :php:class:`Cake\\Controller\\Component`. Failing
184184
to do this will trigger an exception.
185185

186+
Components can use :doc:`/development/dependency-injection` to receive services
187+
as constructor parameters::
188+
189+
namespace App\Controller\Component;
190+
191+
use Cake\Controller\Component;
192+
use App\Service\UserService;
193+
194+
class SsoComponent extends Component
195+
{
196+
public function __construct(
197+
ComponentRegistry $registry,
198+
array $config = [],
199+
UserService $users
200+
) {
201+
parent::__construct($registry, $config);
202+
$this->users = $users;
203+
}
204+
}
205+
206+
.. versionadded: 5.1.0
207+
DI container support for Components was added.
208+
186209
Including your Component in your Controllers
187210
--------------------------------------------
188211

0 commit comments

Comments
 (0)