S
S
Sergey Pozdnyakov2017-06-09 16:40:41
Zend Framework
Sergey Pozdnyakov, 2017-06-09 16:40:41

How to properly replace ServiceLocator in ZF3?

Good day!
When upgrading from ZF2 to ZF3, a ServiceLocatorAware deprecation error appears, since I use ServiceLocator in the controller, in services. Here is one example of solving this problem - to make a factory and specify dependencies in it. Everything seems to be logical.

// Класс фабрики
class IndexControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, 
                     $requestedName, array $options = null) 
    {
        // Извлечь экземпляр сервиса CurrencyConverter из менеджера сервисов.
        $currencyConverter = $container->get(CurrencyConverter::class);
        
        // Создать экземпляр контроллера и передать сервис в его конструктор.
        return new IndexController($currencyConverter);	
    }

Please explain what should I do if the controller has about 10 dependencies? All to push in the designer? It's not very clear after using ZF2.
Let me give you an example, I have an order management controller, it has mappers (I don’t use the doctrine) Order, Tabular part of the order, Discounts, Counterparties, Contracts. As well as services - work with 1C and sending messages to the mail.
If you remove the ServiceLocator from the controller, you need to create private variables for each mapper and services, then create a constructor in which to insert the objects received from the service manager.
To what size can a constructor grow? Not all mappers and services are used in every action, why do I need extra?
All googling on the Internet comes down to a simple example of one dependency, and how to use not a word in large projects (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OnYourLips, 2017-06-09
@Afinogen

if the controller has about 10 dependencies? All to push in the designer?
Separate classes and dependencies. Otherwise, slide down to the magentoo level.
The controller shouldn't have that many.
To what size can a constructor grow?
1-3 external services, except for technical ones (like loggers, transfers, etc.).
how to use it in big projects
SRP.

N
novrm, 2017-06-10
@novrm

Migration Guide

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question