1

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?

2 Answers 2

0

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();
 }
}?>
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.