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;
    }
}
  • In which page, you want to use predispatch? – Rohan Hapani Jan 4 at 7:12
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>
  • getting the same issue not working after refresh and second login – fernandus Jan 4 at 9:43

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.