P
P
P7472021-11-29 17:31:46
symfony
P747, 2021-11-29 17:31:46

Symfony, what is the best way to store a class in settings so that its dependencies are loaded?

Good afternoon!
Please tell me, let's say there are a number of classes whose constructors declare properties of different types: repositories, interfaces, other classes, etc. Through the controller, this is all automatically "pulled up". I need to store these classes in a configuration (eg config.yml). How can I make it so that when an instance of a class is created, all its dependencies are "pulled up"?

Class example:

class Handler
{
    public function __construct(
        TestRepository $testRepository,
        EntityManagerInterface $em,
        Test Class $testClass
    )
    {
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2021-12-10
@P747

It’s not very clear from the question, but after discussion in the comments, it became clear that we need a Service Locator
https://symfony.com/doc/6.0/service_container/serv...

App\handler\action\Handler:
    arguments: !service_locator
        type1: '@App\handler\create\Handler'
        type2: '@App\handler\upload\Handler'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question