0

I want to restrict some unwanted users to access the site. So I am redirecting them to 404 Page but my logic is going to an infinite loop as the event I am calling from controller_action_predispatch is called several times and I am not able to find the right one.

My Logic

namespace Namespace\Module\Observer;

class RedirectUsers implements \Magento\Framework\Event\ObserverInterface {

    protected $_myHelper;

    public function __construct(
        \Namespace\Module\Helper\Data $myHelper,
    ) {
        $this->_myHelper = $myHelper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        if(in_array($currentUser, $restrictedUsers)){
            $url = $this->_myHelper->getNoRoutePageUrl();
            header('Location: ' . $url);
            exit;
        }
    }
}

My event.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer instance="Namespace\Module\Observer\RedirectIp" name="namespace_module_observer_controller_actionpredispatch_controller_action_predispatch"/>
    </event>
</config>
0

Below one you can follow

namespace Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class YourObserver implements ObserverInterface {

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

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

    public function execute(\Magento\Framework\Event\Observer $observer) {

        $actionName = $observer->getEvent()->getRequest()->getFullActionName();
        $controller = $observer->getControllerAction();

        $this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl());
    }
}
  • Can you explain what is it doing or how I should implement this – Priyank Oct 14 '16 at 10:05
  • Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect – Ankit Shah Oct 14 '16 at 10:15
  • I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result – Priyank Oct 14 '16 at 10:22
  • Try to remove ur condition & Check. Please also give Ur events.xml file as well – Ankit Shah Oct 14 '16 at 10:32
  • event xml added – Priyank Oct 14 '16 at 10:37
0

set obeject :

//@var \Magento\Backend\Model\View\Result\Redirect $resultRedirect 
    $resultRedirect = $this->resultRedirectFactory->create();

then just use setPath() method

return $resultRedirect->setPath('*/*/');

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.