我在Symfony 2框架中遇到eventSubscribers
的问题。我写了这段代码:
src/MyBundle/UserBundle/EventListener/UsernameProfileEditExternalListener.php
class UsernameProfileEditExternalListener implements EventSubscriberInterface {
protected $userExternalForumRepository;
protected $userExternalDatesRepository;
public function __construct(UserExternalForumRepository $userExternalForumRepository, UserExternalDatesRepository $userExternalDatesRepository) {
$this->userExternalForumRepository = $userExternalForumRepository;
$this->userExternalDatesRepository = $userExternalDatesiRepository;
}
public static function getSubscribedEvents() {
return array(
'mybundle.user.change_username' => 'onEditUsername',
);
}
public function onEditUsername(FormEvent $event) {
die('It\'s working!');
}
我将此代码添加到文件src/MyBundle/UserBundle/Resources/config/services.yml
:
username_profile_edit_listener:
class: MyBundle\UserBundle\EventListener\UsernameProfileEditExternalListener
arguments: [@user_external_forum_repository, @user_external_dates_repository]
tags:
- { name: kernel.event_subscriber, event: mybundle.user.change_username, method: onEditUsername }
在此文件中src\AppBundle\Controller\Backend\UserController.php
我发送此事件:
$dispatcher = $this->get('event_dispatcher');
$event = new FormEvent($usernameForm, $request);
$dispatcher->dispatch('mybundle.user.change_username', $event);
但它不起作用。我现在完全不知道为什么。我将非常感谢你的建议。我是Symfony的初学者。 services.yml
文件可以缓存吗?