Symfony - 从自定义访问服务 Class
Symfony - Accessing Service from Custom Class
我正在尝试使用 Monolog 库从我创建的名为 Common 的自定义 Class 中记录信息。
我已经将记录器作为参数包含在 service.yml 中 class
parameters:
services:
appbundle.helper.common:
class: AppBundle\Helpers\Common
arguments: ['@doctrine','@logger']
我还为 class 初始化了记录器接口。
private $dataEntityManager;
private $suppEntityManager;
private $curation;
private $innerDoctrine;
private $logger;
/**
* Common constructor.
* @param ManagerRegistry $doctrine
*/
public function __construct(ManagerRegistry $doctrine, LoggerInterface $logger)
{
$this->dataEntityManager = $doctrine->getManager();
$this->suppEntityManager = $doctrine->getManager('gtsupp');
$this->curation = new Curation($doctrine);
$this->innerDoctrine = $doctrine;
$this->logger = $logger;
}
public function detail($a, $b, $c, $d, $e, $f = "", $g = true, $h = true)
{
$this->logger->error('Type not supplied in Common detail ' . __LINE__ . " for descriptor: " . $b);
}
问题是每次我想使用class::Common 我必须在Class的构造函数中提供记录器。
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$common = new Common($this->getDoctrine(), $this->get('logger'));
$common->detail('a', 'b', 'c', 'd', 'e');
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir') . '/..') . DIRECTORY_SEPARATOR,
]);
}
}
P.S。
同样的问题也发生在学说上。如上所示,我每次调用 Common class 时都必须传递它。
改变这个:
$common = new Common($this->getDoctrine(), $this->get('logger'));
对此:
$common = this->get('appbundle.helper.common');
按照您的方式,您没有使用依赖项注入,好的方法是您不需要传递所有参数来实例化您的服务
https://symfony.com/doc/current/components/dependency_injection.html
我正在尝试使用 Monolog 库从我创建的名为 Common 的自定义 Class 中记录信息。
我已经将记录器作为参数包含在 service.yml 中 class
parameters:
services:
appbundle.helper.common:
class: AppBundle\Helpers\Common
arguments: ['@doctrine','@logger']
我还为 class 初始化了记录器接口。
private $dataEntityManager;
private $suppEntityManager;
private $curation;
private $innerDoctrine;
private $logger;
/**
* Common constructor.
* @param ManagerRegistry $doctrine
*/
public function __construct(ManagerRegistry $doctrine, LoggerInterface $logger)
{
$this->dataEntityManager = $doctrine->getManager();
$this->suppEntityManager = $doctrine->getManager('gtsupp');
$this->curation = new Curation($doctrine);
$this->innerDoctrine = $doctrine;
$this->logger = $logger;
}
public function detail($a, $b, $c, $d, $e, $f = "", $g = true, $h = true)
{
$this->logger->error('Type not supplied in Common detail ' . __LINE__ . " for descriptor: " . $b);
}
问题是每次我想使用class::Common 我必须在Class的构造函数中提供记录器。
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$common = new Common($this->getDoctrine(), $this->get('logger'));
$common->detail('a', 'b', 'c', 'd', 'e');
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir') . '/..') . DIRECTORY_SEPARATOR,
]);
}
}
P.S。 同样的问题也发生在学说上。如上所示,我每次调用 Common class 时都必须传递它。
改变这个:
$common = new Common($this->getDoctrine(), $this->get('logger'));
对此:
$common = this->get('appbundle.helper.common');
按照您的方式,您没有使用依赖项注入,好的方法是您不需要传递所有参数来实例化您的服务
https://symfony.com/doc/current/components/dependency_injection.html