0

I am getting 'ERR_TOO_MANY_REDIRECTS'. I tried solution from this link: Magento2: redirection from Observer

 public function execute(Observer $observer)
{
    $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
    $controller = $observer->getControllerAction();
    $this->redirect->redirect($controller->getResponse(), 'sales/order/history');
}

Edit:

This issue was coming because I have used contoller predispatch events so every controller is redirecting to 'sales/order/history', which include sales/order/history controller, which results in too many Redirects

2
  • Hello Atul, Can you please tell me which observer you are using?
    – Tushar
    Commented Sep 22, 2021 at 13:19
  • controller_action_predispatch - I am using this event.
    – explorer
    Commented Sep 23, 2021 at 6:03

1 Answer 1

0

I'll tell you two ways to redirect from the observer.

1)

$controller = $observer->getControllerAction();
    $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
$controller = $observer->getControllerAction();
$url = $this->getUrl('customer/account/login'); //Magento\Framework\UrlInterface $url
$controller->getResponse()->setRedirect($url);

Example

<?php
namespace Magento\Captcha\Observer;

use Magento\Framework\Event\ObserverInterface;

class CheckForgotpasswordObserver implements ObserverInterface
{
    protected $redirect;
    public function __construct(
        \Magento\Framework\App\Response\RedirectInterface $redirect
    ) {
        $this->redirect = $redirect;
    }

    /**
     * Redirect to login page
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $controller = $observer->getControllerAction();
        $this->redirect->redirect($controller->getResponse(), 'customer/account/login');

        return $this;
    }
}

Checked this one,

protected $actionFlag; 
protected $redirect; 

public function __construct(
    \Magento\Framework\App\ActionFlag $actionFlag,
    \Magento\Framework\App\Response\RedirectInterface $redirect

) {
    $this->actionFlag = $actionFlag;
    $this->redirect = $redirect;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
    $this->redirect->redirect($controller->getResponse(), '*/*/edit');
}

This will work for you, Happy Coding

16
  • it's not working for me. showing error - 'too many redirects'
    – explorer
    Commented Sep 23, 2021 at 6:04
  • Okay, Can you please tell me which event you are using?
    – Tushar
    Commented Sep 23, 2021 at 6:07
  • controller_action_predispatch - I am using this event. also please test on magento 2.4.3. as this was working before on previous versions.
    – explorer
    Commented Sep 23, 2021 at 6:09
  • I thing you have to used controller_action_postdispatch because of if you used predispatch it means, Your observer call before, after you observer again magento need to going to check main controller action, So it'll display to many redirect,
    – Tushar
    Commented Sep 23, 2021 at 6:09
  • Okay, Let me check,
    – Tushar
    Commented Sep 23, 2021 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.