FOSUserBundle(Symfony2)。我有一些问题

时间:2014-04-24 09:10:21

标签: php symfony fosuserbundle

1)如何在表格和注册操作中添加新字段(在reg页面上显示新字段)?例如:我想添加新字段last_name,age。

2)我为REGISTRATION_COMPLETED

添加了新的监听器

/src/Acme/UserBundle/EventListener/RegistrationCompletedListener.php:

<?php
namespace Acme\UserBundle\EventListener;

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
 */
class RegistrationCompletedListener implements EventSubscriberInterface
{
    private $router;

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

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

    public function onRegistrationCompletedSuccess(FormEvent $event)
    {
        $url = $this->router->generate('homepage');

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

/src/Acme/UserBundle/Resources/config/services.yml:

services:
    acme_user.registration_completed:
        class: Acme\UserBundle\EventListener\RegistrationCompletedListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

为什么不工作?

1 个答案:

答案 0 :(得分:1)

1)您应该扩展Base User类并在那里添加新字段,如下所示:

namespace Your\CustomBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{    
    /**
     * @var string
     *
     * @ORM\Column(name="first_name", type="string", length=255)
     */
    private $firstName;

    /**
     * @var string
     *
     * @ORM\Column(name="last_name", type="string", length=255)
     */
    private $lastName;

}

并更新appconfig/config.yml

#FOSLUserBundle Configuration
fos_user:
    user_class: Your\CustomBundle\Entity

然后您需要扩展和配置新的注册表单。这是link如何做到这一点。

<强> [编辑]:

<强> 2)

像这样创建事件监听器:

namespace Your\Bundle\EventListener;

use FOS\UserBundle\Event\FilterUserResponseEvent;

class UserListener
{

    public function onRegistrationCompleted(FilterUserResponseEvent $event){

        $user = $event->getUser();
        //do sth....

    }
}

为此注册服务:

services:
    some_name.security.registration_listener:
        class: Your\Bundle\EventListener\UserListener
        tags:
            - { name: kernel.event_listener, event: fos_user.registration.completed, method: onRegistrationCompleted }