0

I have created a observer controller_action_predispatch and did an echo its triggering on all page view but with Second time loading its not triggering on cms_page_view for all other pages it triggers.

Dont know whats the reason, I can guess that it can be cached date, Using a weltpixel pearl theme.

How to fix this.

events.xml

<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;
    }
}
0

This happened Because of the Cache, When I disable Page Cache in admin it works, Found a alternative way to achieve my need Using customer_login.

You can also disable by cacheable="false" in block

<block class="Block\Class" name="blockname" cacheable="false" />

Doing so will slow down customer experience, So go with an alternative fix.

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.