symfony3 Configuration Use fully qualified class name (FQCN) as service id


Example

In many examples, you will find a service id like 'acme.demo.service.id' (a string with dots). You services.yml will look like this:

services:
    acme.demo.service.id:
        class: Acme\DemoBundle\Services\DemoService
        arguments: ["@doctrine.orm.default_entity_manager", "@cache"]

In your controller, you can use this service:

$service = $this->get('acme.demo.service.id');

While there is no issue with this, you can use a Fully Qualified Class Name (FQCN) as service id:

services:
    Acme\DemoBundle\Services\DemoService:
        class: Acme\DemoBundle\Services\DemoService
        arguments: ["@doctrine.orm.default_entity_manager", "@cache"]

In your controller you can use it like this:

use Acme\DemoBundle\Services\DemoService;
// ..
$this->get(DemoService::class);

This makes your code better to understand. In many cases it makes no sense to have a service id that isn't just the class name.

As of Symfony 3.3, you can even remove the class attribute if you service id is a FQCN.