How to Redirect from Plugin in Magento 2

custommagento2magento2.2.3pluginredirect-url

I am trying to redirect to custom URL by using plugin feature of Magento 2 but somehow I am not able to redirect it. For this, I tried a couple of options like,

Magento2: redirection from Observer

Magento2: Redirect Using Plugin (With Website Restrictions ON)

But couldn't succeed with this. I am able to do it If I am adding exit(); after redirect method but it is not good practice to write exit();. Do anyone have some hint on this?

I am using around plugin and before plugin

I have written a plugin on Magento\Customer\Model\AccountManagement class.

Thanks

Best Answer

As per as, our discussion, I am sharing an idea that might help you.

  • First, pass a registry variable from initiatePasswordReset that registry variable will contain your desired URL.
  • Then at controller_action_postdispatch_customer_account_forgotPasswordPostevent catch that registry variable value, that page

Create Plugin over initiatePasswordReset call and use after, before ,around method depend on your choose.

Here, i am using around method.

<?php
/**
 * Created by Amit Bera.
 * User: Amit Kumar Bera
 * Email: dev.amitbera@gmail.com
 * Date: 19-05-2018
 * Time: 17:03
 */

namespace StackExchange\Works\Plugin;


use Magento\Framework\Registry;

class InitiatePasswordResetPlugin
{
    protected $registry;
    public function __construct(
        Registry $registry
    )
    {
        $this->registry = $registry;
    }
    public function aroundInitiatePasswordReset(
        \Magento\Checkout\Model\Session $subject,
        \Closure $proceed,
        $email,
        $template,
        $websiteId
    )
    {
        // Do business logic here
        if($this->registry->registry('my_custom_redirect')){
            $this->registry->unregister('my_custom_redirect');
        }
        $this->registry->register('my_custom_redirect','{YourUrl}');
        return $proceed($email, $template, $websiteId);

    }

}

Now, controller_action_postdispatch_customer_account_forgotPasswordPost event,

redirect that URL my_custom_redirect.

<?php

namespace StackExchange\Works\Observer\Frontend;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Registry;

class ControllerActionPostdispatchForgotPasswordPost implements ObserverInterface
{

    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    protected $actionFlag;

    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
         Registry $registry
    ) {
         $this->registry = $registry;
        $this->actionFlag        = $actionFlag;
    }

    public function execute(Observer $observer)
    {

        $action = $observer->getEvent()->getControllerAction();

        if($this->registry->registry('my_custom_redirect')){
            $this->actionFlag->set('', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH, true);
            $action->getResponse()->setRedirect($this->registry->registry('my_custom_redirect'));
            return $this;
        }
        return $this;
    }
}
Related Topic