How to perform a redirect in Enlight_Event_EventArgs object subscriber in Shopware 5?

559 Views Asked by At

I have a subscriber to the event

'Shopware_Modules_Admin_SaveRegister_Successful'

and after certain actions, I want to perform a redirect in the end, but the $args object does not have a subject which means that I cannot call the $controller = $args->getSubject(); on it and perform a redirect.

Is there another way to perform a redirect inside of the callback function of this event? 'Shopware_Modules_Admin_SaveRegister_Successful' => 'onRegisterSuccessful'

public function onRegisterSuccessful($args){
         $controller = $args->getSubject();
         $controller->redirect(
                array(
                    'controller' => 'profile',
                    'action' => 'verify',
                    'name' => $name
                )
            );
}
2

There are 2 best solutions below

1
alpham8 On

Your option would be to go a bit higher and register an controller's postDispatch event. There you can redirect.

0
cili On

Try like this. Note that you need to exit() after the redirect, because after the event is fired, Shopware makes its own redirect to the customer dashboard.

public static function getSubscribedEvents()
{
    return [
        'Shopware_Modules_Admin_SaveRegister_Successful' => 'onregister'
    ];
}

public function onRegister(\Enlight_Event_EventArgs $args)
{
    $url = Shopware()->Front()->Router()->assemble([
            'controller' => 'profile',
            'action' => 'verify'
    ]);

    header('Location: ' . $url, true);
    exit();
}