0

I have created a Observer controller_action_predispatch which triggers in all page.

Some thing happens strange with this When I clear all cache and login for the first time the observer triggers in home page but when I refresh or relogin it not triggering.

But it works in all other page even after refresh or second login it triggers.

How can I fix this.

I have made a echo of Action Name which comes on first login after cache clear.

sudo php bin/magento cache:clean;
sudo php bin/magento cache:flush;

enter image description here

For Second refresh and second login its not working

enter image description here

events.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 name="restrict_website" instance="Test\Dev\Model\RestrictWebsite" />
    </event>
</config>

RestrictWebsite.php

<?php


namespace SahiSdi\Dev\Model;

use Magento\Customer\Model\Context;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

class RestrictWebsite implements ObserverInterface
{
    protected $_storeManager;

    /**
     * RestrictWebsite constructor.
     */
    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\App\Response\Http $response,
        \Magento\Framework\UrlFactory $urlFactory,
        \Magento\Framework\App\Http\Context $context,
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        $this->_response = $response;
        $this->_urlFactory = $urlFactory;
        $this->_context = $context;
        $this->_actionFlag = $actionFlag;
        $this->_storeManager = $storeManager;
    }

    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $request = $observer->getEvent()->getRequest();
        $actionFullName = strtolower($request->getFullActionName());
        echo $actionFullName;
    }
}
2
  • In which page, you want to use predispatch? Commented Jan 4, 2019 at 7:12
  • @fernandus did you found any solution for this? I am also facing the same issue Commented Feb 27, 2021 at 11:32

4 Answers 4

0

Use like this and flush cache:

<?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_cms_index_index">
        <observer name="restrict_website" instance="Test\Dev\Model\RestrictWebsite" />
    </event>
</config>
1
  • getting the same issue not working after refresh and second login
    – fernandus
    Commented Jan 4, 2019 at 9:43
0

Try controller_front_send_response_before event instead.

Actually there are no other events fired on all pages including CMS without cache issues. So there is no choice

1
  • 3
    Please give more detail of why you suggest using controller_front_send_response_before
    – Amit Bera
    Commented Aug 30, 2019 at 9:20
0

For testing you have to check the impact on frontend in a fresh incognitive window every time. As the below 2 line set the the current guest comtomer session with a redirection, even you remove redirection from a page later on, it will contineu to redirct to customer login page due to guest customer session.

$this->_customerSession->setBeforeAuthUrl($this->_urlInterface->getCurrentUrl());

$this->_customerSession->authenticate();

So just take care while testing the result of this moudle, It will not good okay if you do not use fresh incognitive window, and cache flush command

0

file:- etc/events.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 name="yourvendor_yourmodule_observer_controller_actionpredispatch_controller_action_predispatch" instance="YourVendor\YourModule\Observer\Controller\ActionPredispatch"/>
</event>

file:- YourVendor\YourModule\Observer\Controller\ActionPredispatch.php

    <?php
    namespace [Vendor]\[Module]\Observer\Controller\;

    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;

    class ActionPredispatch implements ObserverInterface{
        public function execute(Observer $
            $controller = $observer->getControllerAction();
        }
    }

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.