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
    Sep 22, 2021 at 13:19
  • controller_action_predispatch - I am using this event. 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

15
  • it's not working for me. showing error - 'too many redirects' Sep 23, 2021 at 6:04
  • Okay, Can you please tell me which event you are using?
    – Tushar
    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. 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
    Sep 23, 2021 at 6:09
  • Okay, Let me check,
    – Tushar
    Sep 23, 2021 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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