2

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...');
  1. Is this the best method?
  2. Should the class be inside a 'Service' directory within the bundle as I have done?
  3. What is the DependencyInjection folder for?

Thanks

1 Answer 1

3

Dependency Inction means that you pass objects into a class, instead of initializing it in the class. A Service Container is a class which helps you managing all these services (classes which have dependencies).

Your questions:

Is this the best method?

Yes, except for the namespace.

Should the class be inside a 'Service' directory within the bundle as I have done?

No, it can live in any namespace. You should put it in a logical namespace, such as MyBundle\Logger.

What is the DependencyInjection folder for?

It's meaned for 3 types of classes: Extension, Configuration and Compiler passes.

Sign up to request clarification or add additional context in comments.

7 Comments

Services are just normal claasea that are put in a service container. So you should just handle it as normal classes and thus you should put them in a descriptive namespace
OK can you please give me an example just to confirm? Would xyz\Bundle\CoreBundle\Logger be appropriate?
Sorry, one more thing. What would you call the class? Just Admin? AdminLogger? AdminLogLogger? AdminLog is already taken for the Entity class.
because you are in a namespace, you can just use AdminLog. I would call it an AdminLogger
Pass classes into a class.. No, pass objects into an object :-) Without a container you can't have DI? That's absolute rubbish! Where do you get this information?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.