I am new to Slim Framework.
There is this dependency container. What is that? How is it used? And can we avoid or not use that?
Slim uses a dependency container to prepare, manage, and inject application dependencies. Unlike its previous version, Slim 4 no longer ships with or depends on a dependency container to work and is now totally optional. If you decide your application requires one, you need to bring it in yourself provide an instance of it to AppFactory before creating your App.
I personally use League Container
$container = new League\Container\Container;
Slim\Factory\AppFactory::setContainer($container);
$app = Slim\Factory\AppFactory::create();
You add dependencies to your container like so;
$container->set('myService', function () {
$myService = new MyService();
return $myService;
});
which then allows you to fetch dependencies from your container from inside a Slim application route like this:
$app->get('/foo', function (Request $request, Response $response, $args) {
$myService = $this->get('myService');
// ...do something with $myService...
return $response;
});
You can read the current docs for v4 here