Magento 2: Get Current URL & Base URL

This article shows how we can get current and base URL in Magento 2. Both Dependency Injection and Object Manager ways of coding are given below. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of StoreManagerInterface & UrlInterface in the constructor of my module’s block … Read more

Magento 2: Get Current Category & Current Product

This article shows how we can get current category and current product data in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Framework\Registry class in the constructor of my module’s block class. app/code/Chapagain/HelloWorld/Block/HelloWorld.php <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_registry; public function __construct( … Read more

Magento 2: Writing Custom Log

In Magento 1, you could simply write your own custom log with the following code: Mage::log("Your Log Message", null, "your_log_file.log"); and it would be saved at MAGENTO_ROOT/var/log/your_log_file.log However, it’s not that simple and quite different in Magento 2. As Magento 2 uses dependency injection design pattern, you need to pass the logger instance through the … Read more