From ba1d9e0054e31de6801eaccec2090a5b58ea9ff4 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Fri, 4 Oct 2024 19:03:27 +0200 Subject: [PATCH 1/2] add more doc for component DI --- en/controllers/components.rst | 7 ++--- en/development/dependency-injection.rst | 41 +++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/en/controllers/components.rst b/en/controllers/components.rst index cd5d3b7385..44ec1be50a 100644 --- a/en/controllers/components.rst +++ b/en/controllers/components.rst @@ -147,11 +147,8 @@ in your controller, you could access it like so:: properties they share the same 'namespace'. Be sure to not give a component and a model the same name. -.. warning:: - - Component methods **don't** have access to :doc:`/development/dependency-injection` - like Controller actions have. Use a service class inside your controller actions - instead of a component if you need this functionality. +.. versionchanged:: 5.1.0 + Components are able to use :doc:`/development/dependency-injection` to receive services. .. _creating-a-component: diff --git a/en/development/dependency-injection.rst b/en/development/dependency-injection.rst index 132beeeefb..b1728b8120 100644 --- a/en/development/dependency-injection.rst +++ b/en/development/dependency-injection.rst @@ -14,10 +14,14 @@ CakePHP will use the :term:`DI container` in the following situations: * Constructing controllers. * Calling actions on your controllers. +* Constructing Components. * Constructing Console Commands. * Constructing Middleware by classname. -A short example would be:: +Controller Example +================== + +:: // In src/Controller/UsersController.php class UsersController extends AppController @@ -45,7 +49,10 @@ database. Because this service is injected into our controller, we can easily swap the implementation out with a mock object or a dummy sub-class when testing. -Here is an example of an injected service inside a command:: +Command Example +=============== + +:: // In src/Command/CheckUsersCommand.php class CheckUsersCommand extends Command @@ -76,6 +83,36 @@ a whole to the Container and add the ``UsersService`` as an argument. With that you can then access that service inside the constructor of the command. +Component Example +================= + +:: + + // In src/Controller/Component/SearchComponent.php + class SearchComponent extends Command + { + public function __construct( + ComponentRegistry $registry, + private UserService $users + ) { + parent::__construct($registry, []); + } + + public function something() + { + $valid = $this->users->check('all'); + } + } + + // In src/Application.php + public function services(ContainerInterface $container): void + { + $container->add(SearchComponent::class) + ->addArgument(ComponentRegistry::class) + ->addArgument(AlfredFTP::class); + $container->add(UsersService::class); + } + Adding Services =============== From 1f7837c7d9cf925d374d86bc032eea4db2dbc88f Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sat, 5 Oct 2024 10:15:44 +0200 Subject: [PATCH 2/2] Update en/development/dependency-injection.rst Co-authored-by: ADmad --- en/development/dependency-injection.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/development/dependency-injection.rst b/en/development/dependency-injection.rst index b1728b8120..bb307fb4c9 100644 --- a/en/development/dependency-injection.rst +++ b/en/development/dependency-injection.rst @@ -109,7 +109,7 @@ Component Example { $container->add(SearchComponent::class) ->addArgument(ComponentRegistry::class) - ->addArgument(AlfredFTP::class); + ->addArgument(UsersService::class); $container->add(UsersService::class); }