FOSUserBundle的控制器中不存在的服务覆盖

时间:2014-01-10 12:52:11

标签: symfony controller override fosuserbundle

我试图覆盖FOSUserBundle的RegistrationController。我已经关注the documentation并且可以让Symfony使用我的控制器,但是当调用registerAction方法时我收到此错误:

You have requested a non-existent service "fos_user.registration.form"

我修改了DependencyInjection\Configuration类以匹配原始类,但似乎没有添加服务。 FOSUserBundle的文档没有说明这一点,所以我不知道如何正确设置它。

以下是我的文件:

RegistrationController.php

namespace ACME\AuthBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Symfony\Component\HttpFoundation\Request;

class RegistrationController extends BaseController
{
  public function registerAction(Request $request)
  {
    $form = $this->container->get('fos_user.registration.form'); // Source of error
    ...
  }
}

的configuration.php

namespace ACME\AuthBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('fos_user');

        $rootNode
            ->children()
                ->arrayNode('registration')
                    ->addDefaultsIfNotSet()
                    ->canBeUnset()
                    ->children()
                        ->arrayNode('confirmation')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->booleanNode('enabled')->defaultFalse()->end()
                                ->scalarNode('template')->defaultValue('FOSUserBundle:Registration:email.txt.twig')->end()
                                ->arrayNode('from_email')
                                    ->canBeUnset()
                                    ->children()
                                        ->scalarNode('address')->isRequired()->cannotBeEmpty()->end()
                                        ->scalarNode('sender_name')->isRequired()->cannotBeEmpty()->end()
                                    ->end()
                                ->end()
                            ->end()
                        ->end()
                        ->arrayNode('form')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->scalarNode('type')->defaultValue('fos_user_registration')->end()
                                ->scalarNode('name')->defaultValue('fos_user_registration_form')->end()
                                ->arrayNode('validation_groups')
                                    ->prototype('scalar')->end()
                                    ->defaultValue(array('Registration', 'Default'))
                                ->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

1 个答案:

答案 0 :(得分:2)

FOSUserBundle使用表单工厂来创建表单,因此您需要使用..

/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.registration.form.factory');

$form = $formFactory->createForm();