Symfony: service.yaml

In symfony package, you could find this file under : config directory

This file job is to figure out all of the services that should be in the container when symfony load

Services comes from external bundles, and also inject your own custom service,

Let’s look at service.yaml file:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

the point here is autowire: true, which means that any services registered in this file should have the autowiring behavior turned on

And look the key point follow:

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

App\Controller\:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

look at App: resource, that’s means:
Make all classes inside src/ available as services in the container.

that’s why you could put any class inside the src and make it called by symfony

to verify, go to terminal:

$ php bin/console debug:autowiring

as you could see what we had add the controller class before, it has put in autowiring lists

and don’t worry, for better performance:

Services are only Instantiated Once

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

exclude key: if you know that some classes don’t need to be in the container, you can exclude them for a small performance boost in the dev environment only.

And what happend if you need to configure one service ?

Let’s do it:

Step 1 found out your services Id

Services Ids = Class Name

go to your terminal:
$ php bin/console debug:container –show-private

you could find your service Id, for example, for markdown:

App\Service\MarkdownHelper App\Service\MarkdownHelper

Step 2 inject monolog logger service
and in order to inject this sevice and make it autowiring by service.yaml

track back $ php bin/console debug:autowiring

Psr\Log\LoggerInterface
alias to monolog.logger

go to src/ Service/ MarkdownHelper.php

add use Psr\Log\LoggerInterface;

and inject argument LoggerInterface

public function __construct(AdapterInterface $cache, MarkdownInterface $markdown, LoggerInterface $logger)
{
    $this->cache = $cache;
    $this->markdown = $markdown;
    $this->logger = $logger;
}

add this logger info in parse function :

if (stripos($source, 'Spicy') !== false) {
    $this->logger->info('They are talking about spicy again!');
}

Step 3 find your service Information

go to terminal

$ ./bin/console debug:container monolog.logger

here you find Service ID monolog.logger

Step 4 Creating a new Logger Channel

Go to config/ packages, add monolog.yaml:

monolog:
    channels: ['markdown']

once add, go to terminal to clean catch:

$ ./bin/console cache:clear

$ ./bin/console debug:container log

you could see:
[44] monolog.logger.markdown

then this is said you had a new logger service – monolog.logger.markdown!

Step 5 Fetching this non-standard service to service.yaml

go to service.yaml file

add follow

# setup special, global autowiring rules
bind:
    $markdownLogger: '@monolog.logger.markdown'

 

Step 5 Logger object to your controller

go to src/Service/MarkdownHelper.php

public function __construct(AdapterInterface $cache, MarkdownInterface $markdown, LoggerInterface $markdownLogger)
{
    $this->cache = $cache;
    $this->markdown = $markdown;
    $this->logger = $markdownLogger;
}

notice you made changed LoggerInterface $logger to LoggerInterface $markdownLogger

because you had add bind logger object in your service.yaml

because we added it to _defaults, it applies to all our services.

github commit here

*** Thanks to bind, we can define what values are passed to specific argument names. But, we can go further and control what value should be passed for a specific type hint ***