2

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

2 Answers 2

2

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 Commented Jan 12, 2018 at 5:39
  • Sure. Let us know the outcome.
    – P S
    Commented Jan 12, 2018 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 Commented Jan 13, 2018 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.
    – P S
    Commented Jan 13, 2018 at 17:43
  • Yeah this extension is working but having the same issue which was previously in my extension Commented Jan 15, 2018 at 5:39
0

You can get code from the below file path

vendor/magento/module-re-captcha-ui/Model/RequestHandler.php

Here you can see the processError() method where the whole redirect code is written. you can follow the same code in your custom file. below is a sample of how to add the same code in your Observer file.

<?php

namespace VendorName\ModuleName\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\ActionFlag;

class ObserverClassName implements ObserverInterface
{
    protected $_urlInterface;
    protected $actionFlag;

    public function __construct(
        UrlInterface $urlInterface,
        ActionFlag $actionFlag
    ) {
        $this->_urlInterface = $urlInterface;
        $this->actionFlag = $actionFlag;
    }

    public function execute(Observer $observer)
    {
        $customRedirectionUrl = $this->_urlInterface->getUrl('YourPath'); // Like cms/page/index
        $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        $controller = $observer->getControllerAction();
        $response = $controller->getResponse();
        return $response->setRedirect($customRedirectionUrl);
    }
}

Note: In the core where this code is working. They are used controller_action_predispatch event. you can check ReCaptcha related core modules in a vendor.

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.