Answer the question
In order to leave comments, you need to log in
How to access a container in the Slim PHP framework, using an example from the documentation?
An example from the documentation www.slimframework.com/docs/v3/concepts/di.html , we add a service to the container, and it seems to be clear here, we get the container and create our object in it (please correct if it’s wrong)
$container = $app->getContainer();
$container['myService'] = function ($container) {
$myService = new MyService();
return $myService;
};
$app->get('/foo', function ($req, $res, $args) {
$myService = $this->get('myService');
return $res;
});
Answer the question
In order to leave comments, you need to log in
You add your object to the container once, and then you can call it anywhere in the application.
$container = $app->getContainer();
$container['myService'] = function ($container) {
$myService = new MyService();
$myService->code = 123;
return $myService;
};
$myService = $this->myService;
echo $myService->code;
// выведется 123
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question