Trying to understand dependency file in Rob Allen's skeleton framework


#1

I downloaded Rob Allen’s skeleton framework to get a reference of how to structure a Slim 3 project. When I look in the dependencies.php file he has 3 areas. Service providers, Service factories, and Action factories. To me service providers and service factories look like the same thing. Action factories look like what is injected into a controller. Can someone explain these to me?


#2

Perhaps @akrabat will chime in, but you can organize your dependencies however you would like. You are correct, action factories are essentially controllers that only have one method. The advantage of using a single method like that is that often not all of the controller’s methods will have the same dependencies so you can give the class/method only what it needs and nothing it will not use.

I couldn’t tell you if there is any strict difference between a service provider vs a service factory. I tend to think of service providers as being pretty simple to configure new App\Some\Class() while factories needing a function or additional configuration to build. But I really don’t know for sure.

They are all entries in the container, so organize them in whatever manner makes the most sense to you and your app.


#3

A factory is a block of code that instantiates a class. They usually exist because the class being instantiated has dependencies that need locating/creating too.

A service factory and an action factory are the same thing - a service factory creates a service class, an action factory creates an action class. A service provider is just another word for the same thing. Usually factories are registered with a DI container so that the DI container calls them when required.

An action class is a controller that responds to a single route. This means that you can control the dependencies on the class more tightly and results in a smaller class than a controller class that handles say 5 routes.