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;
For Second refresh and second login its not working
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;
}
}