0

I need to redirect the customer to a certain URL from a observer after I log him in with loginById.

How can I redirect?

1
  • If observer has controller object then easily redirect from easly
    – Amit Bera
    Jun 2, 2022 at 6:57

3 Answers 3

0

You can get your current url in the observer using

use Magento\Framework\App\ResponseFactory;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\Response\Http

/**
 * @var UrlInterface
 */
protected $_urlInterface;

/**
 * @var ResponseFactory
 */
protected $responseFactory;

public function __construct(
    ResponseFactory $responseFactory,
    UrlInterface $urlInterface,
    Http $request,
) {
    $this->responseFactory = $responseFactory;
    $this->_urlInterface = $urlInterface;
    $this->request = $request;
}

Get the current url

 $currentUrl = $this->_urlInterface->getCurrentUrl();

Then just filter to get the condition of the current url you want

Then redirect anywhere you want

        $redirectUrl = "any url you want";
        $resultRedirect = $this->responseFactory->create();
        $resultRedirect->setRedirect($redirectUrl)->sendResponse('302');

NB: You can also retrieve the current action name it can be usefull

$fullActionName = $this->request->getFullActionName();
7
  • Can you please post the full classnames so I can test your code?
    – Black
    May 31, 2022 at 7:45
  • Sure : use Magento\Framework\UrlInterface;
    – Claims
    May 31, 2022 at 7:48
  • And what is responseFactory ? You miss many details
    – Black
    May 31, 2022 at 7:48
  • use Magento\Framework\App\ResponseFactory;
    – Claims
    May 31, 2022 at 7:48
  • Ok thx, now please edit your question and add these details.
    – Black
    May 31, 2022 at 7:50
0

I solved it with:

header("Location: $url");
exit;
2
  • exit, is not right idea. Question where you have write call loginById function? Controller or model?
    – Amit Bera
    Jun 2, 2022 at 6:54
  • It is a observer for controller_action_predispatch
    – Black
    Jun 2, 2022 at 7:43
0

If you using Magento\Framework\App\ActionFlag then you redirect from controller_action_predispatch

<?php
namespace VendorName\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    protected $_actionFlag;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    /**
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Response\RedirectInterface $redirect
    ) {
        $this->_actionFlag = $actionFlag;
        $this->messageManager = $messageManager;
        $this->redirect = $redirect;
    }

    /**
     * execute function
     *
     * @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();
            if (MYCondition) {
                $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
                $this->redirect->redirect($controller->getResponse(), 'YourUrlToRedirect');
            }

        return $this;
    }
}
1
  • Thx! Will try this out soon !
    – Black
    Jun 2, 2022 at 11:50

Your Answer

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

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