Lukas Lukac
Lukas Lukac

Reputation: 8346

containerAware and Controller in symfony2

FOSUserBundle profile controller

 use Symfony\Component\DependencyInjection\ContainerAware;
 class ProfileController extends ContainerAware

some functions ok ... but when i try then creat form

$form = $this->createForm

This error appear: Call to undefined method ProfileController::createForm()

BUT when i change it to this:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ProfileController extends Controller

The form is rendered... so ... i dont know how can i add this controller to my class and dont remove the ContainerAware ? :/

//

MY solution ?

instead of containeraware i use

use Symfony\Component\DependencyInjection\ContainerAwareInterface;

And then

class ProfileController extends Controller implements ContainerAwareInterface

But i dont know i cant see a different i am noob now so... is it good solution or i will broke something?

Upvotes: 3

Views: 7578

Answers (3)

Amadu Bah
Amadu Bah

Reputation: 2989

Have a look at this blog Symfony2: Moving Away From the Base Controller by Richard Miller

Upvotes: 2

Cerad
Cerad

Reputation: 48865

To answer your original question,

Replace:

$form = $this->createForm

With:

$form = $this->container->get('form.factory')->create($type, $data, $options);

The createForm method is just a convenience method defined in Symfony\Bundle\FrameworkBundle\Controller\Controller. For various reasons, 3rd party libraries tend not to extend the Controller class. Hence createForm is not available.

The real question is: why are you trying to extend the Profile controller? In most cases it is not necessary. It's better to do your customization by listening to events. That of course assumes you are using the development version of FOSUserBundle.

Upvotes: 6

tomas.pecserke
tomas.pecserke

Reputation: 3260

Controller is already ContainerAware - from Controller declaration:

class Controller extends ContainerAware

Upvotes: 2

Related Questions