私はCommonと呼ばれる作成したカスタムクラスからMonologライブラリの情報をログに記録しようとしています。Symfony - カスタムクラスからのサービスへのアクセス
することは私には私もそのクラスのロガーインタフェースを初期化した
parameters:
services:
appbundle.helper.common:
class: AppBundle\Helpers\Common
arguments: ['@doctrine','@logger']
そのクラスのservice.ymlで引数としてロガーが含まれています。
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 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. doctrineで同じ問題が発生します。上記のようにCommonクラスを呼び出すたびに渡す必要があります。
s未満の答えとしてコンテナからそれを引っ張りますuggestsは伝統的な方法です。あなたがS3.3 +を使用している場合は、public function indexAction(Request $ request、Common $ common)を試してください – Cerad