0

The context

I am refactoring an application in ZF2 that uses scripts to synchronize data with web services and APIs.
Currently each script resides in a controller, that is called through cron jobs running index.php script_route. These controllers are defined in the module config as invokables, have routes attributed to them and a console key in the config, they are essentially procedural.
All business logic currently resides in the controllers, all which extend the same class, a sort of central controller, no factories or interfaces. All entities are anemic and used basically for doctrine only.

The problem

In the process of introducing brand new "auxiliary" routines, small synchronization tasks, i stumbled upon the question of where should i put them? Is the correct approach to make them invokable services, called by a controller? Or should they be a controller themselves? They will most likely be used in a single point in the application (during sync operations), that's why I'm unsure if a service is appropriate. What would be a more "Zend Framework" and "OOP" way of structuring these "tasks"?

Mevryk
  • 3
  • 3
  • Possibly relevant https://stackoverflow.com/questions/3499336/in-mvc-where-do-you-draw-the-line-between-a-controller-and-model/3501048#3501048 – Gordon Mar 30 '23 at 14:32
  • Gordon, thank you very much for the link, its was indeed relevant, and good read. I lacked essential knowledge of the design patterns employed by Zend Framework and your other answers led me the right way. I will post an answer with details as soon as time allows. – Mevryk Apr 19 '23 at 12:39

1 Answers1

0

First of all, stop using invocables, and instead use factories for initialization of your controllers (see here). Trust me on this - otherwise you will hate yourself, when attempting to migrate to Laminas.

The the logic should at least be moved to some kind of proper service classes (that you inject in the constructors of your controllers), with normal public methods. Inside those service method would be where you talking to Doctrine's repositories (for example).

Beyond that, it's hard to tell, because you did not describe what exactly you are trying to organize.

tereško
  • 58,060
  • 25
  • 98
  • 150