2014-01-10 34 views
1

我想覆盖FOSUserBundle的RegistrationController。我已经按照the documentation和可以得到的Symfony用我的控制器来代替,但我正在逐渐时,其registerAction方法被调用此错误:FOSUserBundle的控制器覆盖中不存在的服务

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; 
    } 
} 
+0

您是否在应用程序/ AppKernel.php中激活了FOSUserBundle,并且该应用程序/控制台容器中提供了此服务:debug? –

+0

是的,FOSUserBundle被激活。 'container:debug'中没有列出具体的'fos_user.registration.form',尽管有一些子服务,比如'fos_user.registration.form.factory'。试图使用它们,所以我如何添加一个我需要的? – Jukurrpa

回答

2

FOSUserBundle使用窗体工厂创建表单,所以你需要使用..

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

$form = $formFactory->createForm(); 
+0

这没有窍门来创建表单。如何将新服务添加到控制器,例如,如果我需要使用'fos_user.registration.form.handler'? – Jukurrpa

+0

它有几个部分(服务文件,创建类等),但它们都在文档中,并且它比我可以更好地解释它 - http://symfony.com/doc/current/ book/service_container.html – qooplmao

+0

谢谢,我会研究它。 – Jukurrpa