mustache
mustache

Reputation: 13

What is the main advantage to use service in Symfony?

I'm a newbie on symfony, And I don't understand the advantage of use service instead of write the cose in Controller

For Example, I Have a service that Create Log, with a code like this:

        $path = $root.'/../web';
        $fs->touch($path.'/log.txt');
        $this->file = $path.'/log.txt';
        file_put_contents($this->file, $msg, FILE_APPEND | LOCK_EX);

I can put this login in service with DIC ($fs is FileSystem service), or I can Put this Login on my Controller.

Of course If i Need to log often I have to write the same code. The main advantage is decoupling?

Thanks a lot

Upvotes: 1

Views: 495

Answers (2)

Jacob
Jacob

Reputation: 69

Suppose you have a Bar class which uses BasicLogger.

You have a few ways to get access to this logger, lets start with the most simple option:

<?php 

class Bar
{
    public function bar()
    {
        $logger = new BasicLogger();
        $logger->log("foo");
    }
}

This is bad practice because we are mixing construction logic with application logic. It still works, but it has the following drawbacks:

  • It mixes responsibilities.
  • Bar becomes hard to test and cannot be tested without side effects.
  • We cannot dynamically change loggers (code is less reusable).

To solve these drawbacks, we can instead require our Logger class through the constructor.

Our code now looks like this:

class Bar
{
    private $logger;

    public function __construct(Logger $logger)
    {
        $this->logger = $logger;
    }

    public function bar()
    {
        $this->logger->log("foo");
    }
}

Great, our class is no longer responsible for creating the logger, we can test our code without side effects (and make assertions against how the logger was used) and we can now use any logger we like.

So now we use our new class all over the application.

$logger = new Logger();
$bar = new Bar($logger);

Look familiar? Again we are mixing construction logic with application logic, which we already know is bad.

Not only that, but something even worse is happening here, Code duplication.

Thats right. and every time we want to use our Bar class, the duplication gets worse.

The solution? Use the Service container

Registering your logger as a service would mean that all of your code that needs logging functionality is no longer dependent on your specific logger, responsibilities will not be mixed, code duplication will be reduced and your design will become more flexible.

Upvotes: 1

chalasr
chalasr

Reputation: 13167

The main goal and advantage of services is that keep reusable code and use a DRY approach.

Of course, there is a lot of other advantages that you discover progressively as you use them.

Services are accessible from whatever context of your application that can accesses the service container, not only controllers.

If without the service the few lines of code you give would be duplicated in several methods/contexts, you should keep your service.
Otherwise, delete it and do your logic in the specific method.

I think the better approach to use them is at your own feeling.
Don't try to create services in prevention, use them to solve a real need.

When you have a block of code that is duplicated, you should naturally avoid it by creating a service (or other AbstractController that your controllers can extend and inherit the code block) .

The goal is: Always keep a light code and avoid duplicates as possible.
For that, you can use the powerful services of Symfony, or just use the inheritance of classes and other POO principles.

Upvotes: 0

Related Questions