FOSUserBundle&事件监听

时间:2014-09-05 12:03:28

标签: symfony service fosuserbundle event-listener

我尝试在注册确认后将用户重定向到主页,但它不起作用。这是我的倾听者:

<?php

namespace Acme\UserBundle\EventListener;

use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Listener responsible to change the redirection at the end of the password resetting
 */
class RegistrationConfirmListener implements EventSubscriberInterface
{
    private $router;

    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm'
        );
    }

    public function onRegistrationConfirm(GetResponseUserEvent $event)
    {
        $url = $this->router->generate('_welcome');

        $event->setResponse(new RedirectResponse($url));
    }
}

关于关系的另一个问题,我有两个实体,User和Profile,相关的OneToOne,我需要保存两个实体,我可以达到这个只需使用cascade选项= {persist},但是在配置文件中的user_id列实体等于NULL。这是关系声明: 用户实体:

/**
 * @ORM\OneToOne(targetEntity="Profile", mappedBy="user", cascade={"persist"})
 */
protected $profile;

个人资料实体:

 /**
 * @var User
 *
 * @ORM\OneToOne(targetEntity="User", inversedBy="profile")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

服务:

<service id="acme_user.registration_complete" class="Acme\UserBundle\EventListener\RegistrationConfirmListener">
        <tag name="kernel.event_subscriber"/>
        <argument type="service" id="router" />
    </service>

0 个答案:

没有答案