What I'd like to be able to do from any controller is:
$register = $this->get('register_manager');
return $this->render(
'AcmeUserBundle:Account:register.html.twig',
array(
'form' => $register->getRegistrationForm(),
)
);
And in my template
<form>
{{ form_widget(form) }}
</form>
Here's how I have set up so far
In my Acme/UserBundle/Resources/config/services.yml
I have
parameters:
register_manager.class: AcmeUserBundleManagerRegisterManager
services:
register_manager:
class: %register_manager.class%
arguments: [@form.factory]
In RegisterManager.php
I have
namespace AcmeUserBundleManager;
use AcmeUserBundleFormTypeRegistrationType;
use SymfonyComponentFormFormFactoryInterface;
class RegisterManager
{
protected $formFactory;
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
public function getRegistrationForm()
{
return $this->formFactory->createBuilder(new RegistrationType());
}
}
And in AcmeUserBundleFormTypeRegistrationType
I have:
namespace AcmeUserBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('username','text');
$builder->add('email','email');
$builder->add('password','password');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'AcmeUserBundleEntityUser',
);
}
public function getName()
{
return 'registration';
}
}
I know the RegistrationType()
works as I've had it in a controller. My problem is with setting up RegisterManager
as a service, I can't get the right components in there and I'm not sure where to look.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…