0
votes

After a success registration i would to render to index.html.twig

So in class RegistrationController in the function registerAction i had by default:

 return $this->render('FOSUserBundle:Registration:register.html.twig', array(
        'form' => $form->createView(),
    ));

So i change that to :

return $this->render('AcmeCovoiturageBundle:Index:index.html.twig'), array(
        'form' => $form->createView(),
    ));

but this caused error because i had informations in the index.html.twig from database.

so i rechange that to:

 return $this->render(controller("AcmeCovoiturageBundle:Index:index"), array(
        'form' => $form->createView(),
    ));

but i had this error: Attempted to call function "controller" from namespace "FOS\UserBundle\Controller"

Please review it and inform me how can i solve this error

2

2 Answers

1
votes

Finaly, i found a solution. In First, i try to render to another page from registrationController. and it's not totaly wrong if i don't data showed in this page. one of the contributors of FOSUserBundle reply on a question of this render and he said and i quote: "The login is not handled by FOSUserBundle at all, but by the Symfony security component. this bundle is about providing user management only" So i search for another logic. I create a listener. and i named it in the config.yml (projectname/SourcesFiles/app/config/config.yml)

services:
    fos_user.doctrine_registry:
        alias: doctrine
    sdz_user.registration_complet:
        class: Acme\CovoiturageBundle\Services\RegistrationConfirmListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

so the listener will be listen to the event subscriber from the symfony core. After that, add a folder in your bundle and add a php class named RegistrationConfirmListener:

class RegistrationConfirmListener implements EventSubscriberInterface {

private $router;

public function __construct(UrlGeneratorInterface $router) {
    $this->router = $router;
}

/**
 * {@inheritDoc}
 */
public static function getSubscribedEvents() {
    return array(
        FOSUserEvents::REGISTRATION_SUCCESS  => 'onRegistrationSuccess'
    );
}

public function onRegistrationSuccess(\FOS\UserBundle\Event\FormEvent $event) {
    $url = $this->router->generate('index');
    $event->setResponse(new RedirectResponse($url));
}

}

you will need the uses from FOSUserBundle:

use FOS\UserBundle\FOSUserEvents
use FOS\UserBundle\Event\UserEvent
use Symfony\Component\EventDispatcher\EventSubscriberInterface
use Symfony\Component\HttpFoundation\RedirectResponse
use Symfony\Component\Routing\Generator\UrlGeneratorInterface

be careful, if you don't put the namespace on the top, the symfony don't recognize your class.

namespace Acme\CovoiturageBundle\Services

did you see the "REGISTRATION_SUCCESS"? i take this from the registrationController from the function registraionAction:

$dispatcher->dispatch(FOSUserEvents::**REGISTRATION_COMPLETED**, new FilterUserResponseEvent($user, $request, $response));

so, now try to test your listener if he is working or not by run this command :

php app/console container:debug |  app_user.registration_complet

and you will get this message on the console :

[container] Information for service sdz_user.registration_complet Service Id sdz_user.registration_complet Class Acme\CovoiturageBundle\Services\RegistrationConfirmListener Tags - kernel.event_subscriber () Scope container Public yes Synthetic no Lazy no Synchronized no Abstract no Done.

I don't made a solution for the login. this solution for registration. I think it will be the same logic.

0
votes

It depends where your "index.htl.twig" file is located that you are referring to. I suspect it's in "default/index.html.twig" so try this:

return $this->render('default/index.html.twig'), array(
        'form' => $form->createView(),
    ));

The actual path is: app/Resources/views/default/index.html.twig