Following the official documentation ( here ), I decided to split my services.yaml configuration file into several files for readability.
I then created a file config/services/doctrine_listeners.yaml containing this single service definition :
services:
_defaults:
autowire: true
autoconfigure: true
App\Listeners\BookListener:
tags:
- { name: doctrine.event_listener, event: prePersist }
And I import it like this in my config/services.yaml :
imports:
- { resource: 'services/doctrine_listeners.yaml' }
When proceeding like that, the listener is never instanciated. If I instead declare it directly inside the config/services.yaml it works.
This looks like a bug to me, did I miss something ?
The problem might be that the service is overwritten by the default service file. In it there is a PSR-4 service discovery for all classes in src:
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
This will also register your listeners and it looks like this will overwrite the previous configuration from inside your file.
I would recommend adding Listeners
to the exclude section in services.yaml
and then move service discovery for them to your file instead:
services:
_defaults:
autowire: true
autoconfigure: true
App\Listeners\:
resource: '../../src/Listeners/*'
App\Listeners\BookListener:
tags:
- { name: doctrine.event_listener, event: prePersist }
The most probable reason would be redundancy in adding your services, which doesn't allows to load the services. It would be great if you share you config/services.yml content here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.