如果我没有使用 DI 注入服务,我如何直接从容器中获取服务?
How do I get a service from the container directly, if I didn't/couldn't inject the service using DI?
我有一部分代码通过依赖注入注入两个服务 $checker
和 $paginator
。它完美运行:
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;
}
在 services.yaml
文件的配置下方:
paginator:
public: true
class: 'App\Helper\Paginator'
checker:
public: true
class: 'App\Helper\Checker'
arguments:
$paginator: '@paginator'
但由于某些原因我想通过方法注入服务:
$checker = $this->container->get('checker');
但是没有用。在以前的版本中,Symfony 就像 3.4 一样。
我收到一个错误:
Service "checker" not found: event though it exists in the app's container, the container inside "App\Controller\DefaultController" 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.
我该如何解决?
您需要添加依赖项以便 the service locator can find them。
将方法 getSubscribedServices()
添加到您的控制器 class:
public static function getSubscribedServices()
{
return array_merge(
parent::getSubscribedServices(),
[
'checker' => Checker::class,
]
);
}
如果你的控制器 class 扩展 AbstractController
你可以简单地做:
$this->get('checker');
如果您想在另一种类型的 class 中执行此操作(例如,不扩展 AbstractController
的服务),那么您需要声明您的服务实现了 ServiceSubscriberInterface
.
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Psr\Container\ContainerInterface;
class FooService implements ServiceSubscriberInterface {
public function __construct(ContainerInterface $locator)
{
$this->locator = $locator;
}
public static function getSubscribedServices() { /* same as before */ }
public function get($service)
{
return $this->locator->get($service);
}
}
...您将能够像之前在控制器中那样做。
我有一部分代码通过依赖注入注入两个服务 $checker
和 $paginator
。它完美运行:
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;
}
在 services.yaml
文件的配置下方:
paginator:
public: true
class: 'App\Helper\Paginator'
checker:
public: true
class: 'App\Helper\Checker'
arguments:
$paginator: '@paginator'
但由于某些原因我想通过方法注入服务:
$checker = $this->container->get('checker');
但是没有用。在以前的版本中,Symfony 就像 3.4 一样。
我收到一个错误:
Service "checker" not found: event though it exists in the app's container, the container inside "App\Controller\DefaultController" 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.
我该如何解决?
您需要添加依赖项以便 the service locator can find them。
将方法 getSubscribedServices()
添加到您的控制器 class:
public static function getSubscribedServices()
{
return array_merge(
parent::getSubscribedServices(),
[
'checker' => Checker::class,
]
);
}
如果你的控制器 class 扩展 AbstractController
你可以简单地做:
$this->get('checker');
如果您想在另一种类型的 class 中执行此操作(例如,不扩展 AbstractController
的服务),那么您需要声明您的服务实现了 ServiceSubscriberInterface
.
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Psr\Container\ContainerInterface;
class FooService implements ServiceSubscriberInterface {
public function __construct(ContainerInterface $locator)
{
$this->locator = $locator;
}
public static function getSubscribedServices() { /* same as before */ }
public function get($service)
{
return $this->locator->get($service);
}
}
...您将能够像之前在控制器中那样做。