symfony2中的containerAware和Controller

Blo*_*kas 3 containers extends controller symfony

FOSUserBundle配置文件控制器

 use Symfony\Component\DependencyInjection\ContainerAware;
 class ProfileController extends ContainerAware
Run Code Online (Sandbox Code Playgroud)

一些功能好......但是当我尝试然后创建表格

$form = $this->createForm
Run Code Online (Sandbox Code Playgroud)

出现此错误:调用未定义的方法ProfileController :: createForm()

但是当我把它更改为:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ProfileController extends Controller
Run Code Online (Sandbox Code Playgroud)

表单被渲染...所以......我不知道如何将此控制器添加到我的班级并且不删除ContainerAware?:/

//

我的解决方案

而不是我使用的containeraware

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
Run Code Online (Sandbox Code Playgroud)

然后

class ProfileController extends Controller implements ContainerAwareInterface
Run Code Online (Sandbox Code Playgroud)

但我不知道我现在不能看到一个不同的我是noob ...这是好的解决方案还是我会破坏一些东西?

Cer*_*rad 6

要回答你原来的问题,

更换:

$form = $this->createForm
Run Code Online (Sandbox Code Playgroud)

附:

$form = $this->container->get('form.factory')->create($type, $data, $options);
Run Code Online (Sandbox Code Playgroud)

createForm方法只是Symfony\Bundle\FrameworkBundle\Controller\Controller中定义的便捷方法.由于各种原因,第三方库往往不扩展Controller类.因此createForm不可用.

真正的问题是:你为什么要扩展Profile控制器?在大多数情况下,没有必要.最好通过听事件来进行自定义.当然,假设您使用的是FOSUserBundle的开发版本.