I have a part of code where I'm injecting two services $checker
and $paginator
by dependency injection. It works perfectly:
public function index(Request $request, Paginator $paginator, Checker $checker)
{
$result = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
$partialResult = $paginator->getPartial($result, 0, 3);
$checker->isValid('A');
var_dump("test");
die;
}
Below the configuration on services.yaml
file:
paginator:
public: true
class: 'AppHelperPaginator'
checker:
public: true
class: 'AppHelperChecker'
arguments:
$paginator: '@paginator'
But I'd like to inject for some reasons service by method:
$checker = $this->container->get('checker');
But it doesn't work. In previous versions Symfony like 3.4 it used to.
I'm receiving an error:
Service "checker" not found: event though it exists in the app's container, the container inside "AppControllerDefaultController" is a smaller service locator that only knows about the "http_kernel", "parameter_bag", "request_stack", "router", "session", and "twig" services. Try using dependency injection instead.
How should I solve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…