I am new to Magento 2 modules and would like to create a module to run the below class whenever someone accesses the homepage. I believe it would be easier if I converted this to an observer so I just wanted to confirm that that was as simple as reconfiguring the below code block.
<?php
namespace Webkul\Module\Controller\DemoController;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
class DemoClass extends Action {
private $remoteAddress;
public function __construct(
Context $context,
RemoteAddress $remoteAddress
) {
$this->remoteAddress = $remoteAddress;
parent::__construct($context);
}
public function execute()
{
$ip = $this->remoteAddress->getRemoteAddress();
echo "Visitor's IP = ".$ip;
}
}