我想在注册表单时自动设置密码。所以我使用REGISTRATION_INITIALIZE来触发事件。不幸的是它没有用。
监听器:
import numpy as np
import cv2
name = 'two_eyes1.png'
# reading an image
img = cv2.imread(name, cv2.IMREAD_COLOR)
# inverting image
img_inv = cv2.bitwise_not(img)
gray = cv2.cvtColor(img_inv, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY)
#----- Blob detector parameters initiation
params = cv2.SimpleBlobDetector_Params()
#change thresholds
params.minThreshold = 0;
params.maxThreshold = 255;
#filter by area
params.filterByArea = True
params.minArea = 70
# filter by cicularity
params.filterByCircularity = True
params.minCircularity = 0.1
# filter by convexity
params.filterByConvexity = True
params.minConvexity = 0.87
# filter by inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01
det = cv2.SimpleBlobDetector_create(params)
keypoints = det.detect(img)
im_with_key = cv2.drawKeypoints(img, keypoints, np.array([]),
(0,0,255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#----------
titles = ['Input','Inverted','Grayscaled','Thresholded','blobpart']
images = [img, img_inv, gray, threshold, im_with_key]
for i in range(5):
cv2.imshow(titles[i], images[i])
cv2.waitKey(0)
cv2.destroyAllWindows()
服务:
<?php
namespace Acme\UserBundle\EventListener;
use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RegistrationListener implements EventSubscriberInterface
{
/**
* @var EntityManager
*/
private $em;
/**
* @param \Doctrine\ORM\EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInit',
);
}
public function onRegistrationInit(UserEvent $userEvent)
{
$user = $userEvent->getUser();
$user->setPassword('abcdeffffff');
}
因此密码未设置,并显示密码不应为空白。
我做错了什么?任何帮助!
编辑:
问题是我在错误的地方定义服务。
而不是#src/Acme/UserBundle/Resources/config/services.yml
services:
acme_user.registration:
class: Acme\UserBundle\EventListener\RegistrationListener
arguments:
entityManager: "@doctrine.orm.entity_manager"
tags:
- { name: kernel.event_subscriber}
,它应该是src/Acme/UserBundle/Resources/config/services.yml
。
我在http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html中看到了app/config/services.yml
,但却没有为我工作!
答案 0 :(得分:0)
也许我错了,但我认为您的服务定义使用features from Symfony 3.3,但您使用的是Symfony 3.2.8
,例如
3.3版中的新功能:在Symfony 3.3中添加了通过名称($ adminEmail)配置参数的功能。以前,您只能通过其索引(在本例中为2)或使用其他参数的空引号来配置它。
尝试将服务定义更新为:
#src/Acme/UserBundle/Resources/config/services.yml
services:
acme_user.registration:
class: Acme\UserBundle\EventListener\RegistrationListener
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_subscriber}
还有一句话:当您实际使用订阅者时,您应该将班级RegistrationListener
重命名为RegistrationSubscriber
。