未找到symfony \ bundle \ frameworkbundle \ controller

时间:2014-06-03 12:12:25

标签: php symfony silex

似乎无法创建表单,因为找不到frameworkbundle。我真的不知道如何解决问题。

我尝试使用不同的方法来创建表单,我使用以下链接来完成这项工作http://symfony.com/doc/current/cookbook/doctrine/registration_form.html

<?php
namespace Forms;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) 
{
    $builder->add('username', 'username');
    $builder->add('email', 'email');
    $builder->add('password', 'repeated', array(
        'first_name' => 'password',
        'second_name' => 'confirm',
        'type'      =>  'password',
    ));
}

public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => '\Entities\User'
    ));
}

public function getName()
{
    return 'user';
} 
}

这是构建表单的表单,然后我让控制器在网页上显示表单。

 <?php
 namespace Controllers;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
 use Forms\UserType;

 class RegisterController extends Controller
 {
public function get(Request $request)
{
    $registration = new UserType();
    $form = $this->createForm(new UserType(), $registration, array(
        'action' => $this->generateUrl('account_create'),
    ));
    if($form->isValid())
    {
        $data = $form->getData();
        echo $data;

        return $app->redirect('login');
    }

    return $app['twig']->render('register.twig', array('form' => $form->createView()));
}
}

1 个答案:

答案 0 :(得分:2)

实际上并不知道它是否可以解决您的问题,但代码中存在错误:

$registration = new UserType();
$form = $this->createForm(new UserType(), $registration, array(
    'action' => $this->generateUrl('account_create'),
));

您应该将新的User对象作为第二个参数传递给$this->createForm方法,而不是表单对象。请参阅方法文档here

您应该创建用户对象&amp;然后将其传递给createForm()

$user = new User();
$form = $this->createForm(new UserType(), $user, array(
    'action' => $this->generateUrl('account_create'),
));