简体   繁体   English

即使定义了服务,Symfony事件订阅者也无法正常工作

[英]Symfony event subscriber not working even service is defined

I have an event subscriber: 我有一个活动订阅者:

namespace App\EventListener;

use App\Entity\Token;
use App\Model\EncryptUtils;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Common\EventSubscriber;

class DatabaseSubscriber implements EventSubscriber
{

    public function getSubscribedEvents()
    {
        return array(
            'prePersist'
        );
    }

    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();

        if ($entity instanceof Token) {
            $enityt->setCreatedAt(new \DateTime());
        }
    }
}

And I've declared the service in services.yaml: parameters: locale: 'en' 我已经在services.yaml中声明了该服务:参数:区域设置:'en'

services:

    App\EventListener\DatabaseSubscriber:
        tags:
            - { name: doctrine.event_subscriber, connection: default }

    _defaults:
        autowire: true      
        autoconfigure: true
        public: false       

        bind:
            $appSecret: '%kernel.secret%'

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

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

But there's no way to get it working. 但是没有办法使它工作。 I've also tried with an event listener but nothing happens 我也尝试过使用事件侦听器,但没有任何反应

You should declare your service after all the default configuration because if not, the default configuration is overriding yours. 您应该在所有默认配置之后声明服务,因为如果没有,默认配置将覆盖您的服务。
So your services.yaml file should be: 因此,您的services.yaml文件应为:

services:

    _defaults:
        autowire: true      
        autoconfigure: true
        public: false       

        bind:
            $appSecret: '%kernel.secret%'

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

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

    App\EventListener\DatabaseSubscriber:
        tags:
            - { name: doctrine.event_subscriber, connection: default }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM