私は Symfony2 を初めて使用し、ユーザー管理、ページ管理、画像ライブラリなどのさまざまなセクションを持つカスタム CMS を構築しました。CMS 内のすべてのアクティビティをログに記録したいので、集中化されたクラスを作成するのが最善だと考えました。アクティビティを保存して、どのセクションからでも呼び出せるようにします。
依存性注入とサービス コンテナーを見てきましたが、違いを理解するのに苦労していますか? もしあれば?
次のサービスをセットアップしましたが、これが最善の方法であるかどうかについてフィードバックをお願いします。
# app/config/config.yml
# AdminLog Configuration
services:
admin_log:
class: xyz\Bundle\CoreBundle\Service\AdminLogService
arguments: [@doctrine.orm.entity_manager]
以下は私のクラスです:
<?php
namespace xyz\Bundle\CoreBundle\Service;
use xyz\Bundle\CoreBundle\Entity\AdminLog;
class AdminLogService
{
protected $em;
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->em = $em;
}
public function logActivity($controller, $action, $entityName, $note)
{
$adminLog = new AdminLog(
1,
$controller,
$action,
$entityName,
$note
);
$this->em->persist($adminLog);
$this->em->flush();
}
}
次に、以下を使用して、CMS 内の任意のコントローラーからこれを呼び出します。
$this->get('admin_log')->logActivity('Website', 'index', 'Test', 'Note here...');
- これが最善の方法ですか?
- 私が行ったように、クラスはバンドル内の「サービス」ディレクトリ内にある必要がありますか?
- DependencyInjection フォルダーとは何ですか?
ありがとう