0

I have tried a lot of method to resolv this problem, but it's still not working. What i want, is than after the user success to register he is redirecting to the wanted page.

So, i have a UserBundle with :

  • in UserBundle/EventListener

    namespace MDB\UserBundle\EventListener;
    
    use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use FOS\UserBundle\FOSUserEvents;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    
    class RegistrationConfirmListener implements EventSubscriberInterface {
    
    private $router;
    
    public function __construct(UrlGeneratorInterface $router) {
    
        $this->router = $router;
    }
    
    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents() {
        return array(
            FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm'
        );
    }
    
    public function onRegistrationConfirm(GetResponseUserEvent $event) {
        $url = $this->router->generate('mdb_platform_homepage');
    
        $event->setResponse(new RedirectResponse($url));
    }
    
    }
    
    ?>
    
  • in services.yml (of user bunde)

    services:

            mdb_user.registration.form.type:
                class: MDB\UserBundle\Form\Type\RegistrationFormType
                tags:
                    - { name: form.type, alias: mdb_user_registration }
    
            mdb.user.validator.contains_user:
                class: MDB\UserBundle\Validator\Constraints\ContainsUserValidator
                arguments: [ "@doctrine.orm.entity_manager" ]       
                tags:
                    - { name: validator.constraint_validator, alias: contains_user } 
    
            mdb_user.registration_complet:
                class: MDB\UserBundle\EventListener\RegistrationConfirmListener
                arguments: ["@router"]
                tags:
                    - { name: kernel.event_subscriber }
    

i have tried a lot of other config and check the other forums but it seems the redirection never work.

Anyone have an idea? thanks in advance

LedZelkin
  • 586
  • 1
  • 10
  • 24

3 Answers3

1

I fixed the problem like that :

in app/config/routing :

add this

fos_user_registration_confirmed:
    pattern: /registration/confirmed
    defaults: { _controller: MDBUserBundle:User:confirmed }
    requirements:
        _method: GET

and in the targeted controller do what you want :

  public function confirmedAction()
    {
        return $this->render('MDBPlatformBundle:Default:index.html.twig'
        );
    }

a good link who can help for similar situation : How to customize FOS UserBundle URLs

hope this will help

Community
  • 1
  • 1
LedZelkin
  • 586
  • 1
  • 10
  • 24
0

There are some ways to do that :

Changing the default Page

# app/config/security.yml
security:
    firewalls:
        main:
            form_login:
                # ...
                always_use_default_target_path: true
                use_referer: true
                default_target_path: default_security_target

Control the Redirect URL from inside the Form

{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
    <div>{{ error.message }}</div>
{% endif %}

<form action="{{ path('login_check') }}" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />

    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />

    <input type="hidden" name="_target_path" value="account" />

    <input type="submit" name="login" />
</form>

and in your security :

# app/config/security.yml
security:
    firewalls:
        main:
            form_login:
                target_path_parameter: redirect_url

Read more at : http://symfony.com/doc/current/cookbook/security/form_login.html

Jun Rikson
  • 1,964
  • 1
  • 22
  • 43
  • Thx for your answer but i know this way ,i already did it and it work for the form_login, but i dont know how to do for the register_form. Maybe there is somthing like that : 'form_register: success_path : xxxx; ' ? – LedZelkin Jan 31 '15 at 21:13
-1

You can simplify LeZelkins accepted answer even more if you just want to redirect. Use the FrameworkBundle RedirectController:

# routing.yml
fos_user_registration_confirmed:
    path: /registration/confirmed
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /admin/dashboard
gblock
  • 495
  • 3
  • 13
  • What "doesnt work"? This is copy/paste from a working project of mine. a) You override fosuserbundle routes by redeclaring them. b) Even the symfony documentation gives a similar example of "How to Configure a Redirect without a custom Controller" (http://symfony.com/doc/current/routing/redirect_in_config.html) – gblock Nov 22 '16 at 14:18