Can anybody here show clearly how to integrate Guzzle with Slim 3 using dependency container, so it is directly available in rotexsoft/slim3-skeleton-mvc-app controllers as e.g. “Client” object.
I would really appreciate “for the beginner” type of answer.
Thanks ahead
Habit. Sometimes I need to access something in my settings file to pass to a constructor. For example I use Guzzle in my projects to hook up to services like mailgun. Passing the container into the function gets me access to my app settings where I’d pass API keys and such to the Guzzle client.
Isn’t it an 'antipattern"? I mean - passing the DI container into a function? I would rather pass it’s specific property to a function, not the container itself!
Correct! You don’t need to do it that way though. The docs show a few different methods, note where it says this.
Slim first looks for an entry of \HomeController in the container, if it’s found it will use that instance
You can setup your controller and its dependencies in the container, then the controller is given what it needs, and only what it needs, from the DI. See this example of creating the controller from the DI.