I'm pretty new to Symfony2 and have built a custom CMS which has various sections such as user management, page management, image library etc. I want to log all activity within the CMS, therefore thought it would be best to create a centralised class to store the activity so that I can call it from any section.
I've been having a look at dependency injection and service container but struggling to figure out what the difference is? If any?
I've setup the following service but would like feedback on if this is the best method:
# app/config/config.yml
# AdminLog Configuration
services:
admin_log:
class: xyz\Bundle\CoreBundle\Service\AdminLogService
arguments: [@doctrine.orm.entity_manager]
Below is my class:
<?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();
}
}
I will then call this from any controller within the CMS using the following:
$this->get('admin_log')->logActivity('Website', 'index', 'Test', 'Note here...');
- Is this the best method?
- Should the class be inside a 'Service' directory within the bundle as I have done?
- What is the DependencyInjection folder for?
Thanks