我正在使用symfony2和FOSUserBundle
我想在ProfileController中使用'createFormBuilder'
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Acme\UserBundle\Entity\AttrMutor;
class ProfileController extends ContainerAware
{
$attrMutor = new AttrMutor();
$form = $this->createFormBuilder($attrMutor);
Run Code Online (Sandbox Code Playgroud)
它显示如
FatalErrorException: Error: Call to undefined method Acme\UserBundle\Controller\ProfileController::createFormBuilder()
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
我可以在其他Controller中使用$ this-> createFormBuilder
class DefaultController extends Controller
{
$attrMutor = new AttrMutor();
$form = $this->createFormBuilder($attrMutor); //OK
Run Code Online (Sandbox Code Playgroud)
我认为扩展ContainerAware和扩展Controller之间存在差异
该createFormBuilder方法在Symfony的基Controller类中.(http://api.symfony.com/2.3/index.html?q=createFormBuilder)
如果你只想有你的控制器扩展ContainerAware,而不是Controller,你仍然可以从服务容器表单生成器:
$builder = $this->container->get('form.factory')->createBuilder('form', $data, $options);
Run Code Online (Sandbox Code Playgroud)