Executing a module when the user opens the page

I have a module that records the user's IP on the debug log when he visits the page "example.com/iplog/index/test".

My module is located in the app/code/IpLog folder and contains the modules.xml, registration.php, routes.xml and controller files.

Ip log example:

[2019-05-13 21:53:03] main.DEBUG: 127.0.0.1 05/13/2019 09:53:03 pm [] []

I want this action (IP logging) to happen when the user enters the main page on the site "example.com". How can I execute the module this way?

Solutions

You can check if controller is home page then add your logging for home page controller.

Check this link on mageplaza https://www.mageplaza.com/devdocs/how-check-current-url-homepage-magento-2.html

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{

 protected $_logo;  

 public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Theme\Block\Html\Header\Logo $logo,
    array $data = []
)
{       
    $this->_logo = $logo;
    parent::__construct($context, $data);
}

/**
 * Check if current url is url for home page
 *
 * @return bool
 */
 public function isHomePage()
 {  
    return $this->_logo->isHomePage();
 }
}?>

Using this other answer i was able to understand better what i needed to do. I created a file called "events.xml" and a Observer class. This way a module is able to listen to a specific event and act upon it.

Similar questions

Module installation script isn't executing
I tried to many ways to do this, but isn't executing. app/code/local/Devra/Address/etc/config.xml app/code/local/Devra/Address/sql/address_setup/mysql4-install-0.1.0.php Also, this is the result for core_resources: I also cleaned the cache, so i don't know what to do.
M2.4 Exception throwing while executing composer require: Higher matching version 2.3.x of xxx/module-xxxx was found in public repository packagist
While executing composer require xxxx/module-xxxx I'm facing below error on the console. Repository is private at the moment.
Problem executing shortcode
I created a shortcode that will fetch the content of a certain page but i am having problems with shortcodes that was inside the page. Basically my goal is to retrieve the page content and execute the shortcode inside of it. Thanks in advance. Edits : On my home page i create tabs. The content for each tabs are generated in CMS >> page. here's my c...
Observer not executing. Could use extra eyes
with the help I received here yesterday, I'm trying to set up an observer that will update the "Manage Stock" option of all child items of a configurable product when it is changed. Following quite a few tutorials, I think I have the code right, but the observer isn't firing. My module is setup as follows: The module setup in /app/etc/modules: MyCo...
observer method sales_order_place_after not executing
I am using magento 1.8. I have requirement that need to save the order details in separate custom table. for this I need to execute saveorder method using observer event sales_order_place_after But when I am clicking place order, My observer function is not executing, My config.xml file content my observer method If this information is not enough, ...
Alternative method for executing update queries within Magento using PHP
I have a script that runs outside of Magento to do some "maintenance" work on the database. Here is an example of how it works: Works perfectly until I swap that SELECT query with an UPDATE query. Apparently, this is the reason: https://stackoverflow.com/questions/12979510/pdo-error-sqlstatehy000-general-error-when-updating-database I attempted to ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.