1

I am trying to redirect from observer but it does passing in Magento marketplace validations.

Tried.

  1. $this->redirect->setRedirect($customRedirectionUrl);

  2. $this->_response->create()->setRedirect($url)->sendResponse(); $this->_messageManager->addError($message);

  3. $this->_responseFactory->create()->setRedirect($url)->sendResponse();

I need a redirect method which does not depend on exit and die because magento does not allow that.

2
1

The codes you have mentioned in question do redirect effectively. Since Magento is not allowing to use redirect methods depending upon exit and die I had search for such methods which are already being used in magento core's observer for redirect purpose.

I have found few of them which I'm going to list:

1. Magento\Captcha\Observer\CheckForgotpasswordObserver.php

public function __construct(
    \Magento\Framework\App\Response\RedirectInterface $redirect,
) {
    ...
    $this->redirect = $redirect;
}

/**
 * Check Captcha On Forgot Password Page
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return $this
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $formId = 'user_forgotpassword';
    $captchaModel = $this->_helper->getCaptcha($formId);
    if ($captchaModel->isRequired()) {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $controller = $observer->getControllerAction();
        if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
            $this->messageManager->addError(__('Incorrect CAPTCHA'));
            $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
            $this->redirect->redirect($controller->getResponse(), '*/*/forgotpassword');
        }
    }

    return $this;
}

Since it is included in core file I think this could be the one that can be used.

2. You may see the same method is used in Magento\Captcha\Observer\CheckContactUsFormObserver and module-captcha/Observer/CheckUserEditObserver.php as well.

6
  • Thanks for your response i will try and get back to you with the outcome – Pramod Kumar Sharma Jan 12 '18 at 5:39
  • Sure. Let us know the outcome. – Purushotam Sangroula Jan 12 '18 at 5:40
  • This solution seems to be working with all other Php version -7.0.21 Environment: Operating System - Centos 7, PHP Version - 7.0.21, Magento Platform - 2.0.17 CE Varnish, Sampla Data Command: vendor/bin/phpunit Installation issue: magento.dev/gear.html page doesn't contain HIT in cache header. Fac – Pramod Kumar Sharma Jan 13 '18 at 7:57
  • Okay. Is this the solution you were looking for? It would be great to know if it works. Thanks for your reply. – Purushotam Sangroula Jan 13 '18 at 17:43
  • Yeah this extension is working but having the same issue which was previously in my extension – Pramod Kumar Sharma Jan 15 '18 at 5:39

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.