I have my custom controller that has my custom code, now I need to run that code when any URL hits from the site.
My Controller URL is like
common/index/code run
can anyone help me how can I archive this in Magento ver. 2.3.5-p1
Viral Patel
1,1001 gold badge8 silver badges17 bronze badges
-
Easy way is to use ajax on document loadNilesh Dubey– Nilesh Dubey2021年12月27日 08:24:53 +00:00Commented Dec 27, 2021 at 8:24
1 Answer 1
- create
events.xmlat following location.
app\code\Vendor\Extension\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="my_custom_observer" instance="Vendor\Extension\Observer\MyClass" shared="false" />
</event>
</config>
- Now create
MyClass.phpfile on following location.
app\code\Vendor\Extension\Observer\MyClass.php
<?php
namespace Vendor\Extension\Observer;
use Magento\Framework\Event\ObserverInterface;
class MyClass implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
// THIS EVENT CALL ON EVERY URL
// insert your custom code here, according to your requirement
}
}
answered Dec 27, 2021 at 4:25
Dhiren Vasoya
9,70914 gold badges37 silver badges61 bronze badges
Explore related questions
See similar questions with these tags.
default