Required parameter '$registry' missing, when trying to use Component from Command

I have bit complex routine in web interface to update data and it works as I want. I have created a Component for this logic.

I would like to use that same logic from Command.

I have tried to call Component using following:
$this->TestFunctions = new TestFunctionsComponent(new ComponentRegistry());

That will give me error “Required parameter ‘$registry’ missing”

I have tried to use also direct call to the Component, which give same error.

I suppose one option would be to move whole logic from component to utility class, but that would require quite big refactoring.

Thank you in advance for all the help.

Seem like it is expecting registry at a different place than the first constructor argument. Would specifying registry as a named parameter make it work?

But probably a service class would stop you pulling in the uneeded dependencies.

 $component = new TestComponent(
            registry: new ComponentRegistry(
                new AppController(
                    new ServerRequest()
                )
            )
        );

        $component->hiJames();

If you are on CakePHP 4.2 or higher I would rather highly recommend you create a service class and inject it into your command via dependency injection.

Components are only really suited to be shared across controllers, not anything else.

See https://www.youtube.com/watch?v=z3E_UdH1XeE

1 Like

I ended up moving all required functionality to few service classes to solve this. Took quite some time to do it, because these functions we used quite much in other parts of the application.

@jmcd73 Thank you for suggestion, I will test that even though I already moved to service class

@KevinPfeifer Thank you for good video, I founded that video before I wrote this message and ended up using that video as study material. There was one part, which did not work in my setup (cakePHP 5.0.6). I had to change $this->loadModel() to $this->fetchModel().

I’d say a video from over 2 years ago can contain some breaking changes since you are not using the same CakePHP version