Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 629
Lukas Lukac Avatar asked Aug 14 '26 03:08

Lukas Lukac


1 Answers

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.

like image 71
Cerad Avatar answered Aug 16 '26 12:08

Cerad