While working in Magento, many times there is also a need to get the path of the module’s directory mainly, so in this post, we will show you how to find the path of the module’s directory using the template and controller.
Method 1 Getting directory path using controller:
Firstly, you need to create a Controller File Index.php in your custom module. After creating the controller file, add the code given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace Webiators\GetDir\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Module\Dir; class Index extends Action { public function __construct( Context $context, Dir $moduleDir ) { $this->moduleDir = $moduleDir; parent::__construct($context); } public function execute() { echo $modulePath = $this->moduleDir->getDir('Webiators_GetDir'); echo $moduleEtcPath = $this->moduleDir->getDir('Webiators_GetDir', Dir::MODULE_ETC_DIR); echo $moduleI18nPath = $this->moduleDir->getDir('Webiators_GetDir', Dir::MODULE_I18N_DIR); echo $moduleViewPath = $this->moduleDir->getDir('Webiators_GetDir', Dir::MODULE_VIEW_DIR); echo $moduleControllerPath = $this->moduleDir->getDir('Webiators_GetDir',Dir::MODULE_CONTROLLER_DIR); echo $moduleSetupPath = $this->moduleDir->getDir('Webiators_GetDir',Dir::MODULE_SETUP_DIR); } } |
Method 2 Getting directory path using Template/Phtml File:
Create a template file getdir.phtml in your custom module, and add this code given below
1 2 3 4 5 6 7 8 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $fileSystem = $objectManager->create('\Magento\Framework\Module\Dir'); $modulePath = $fileSystem->getDir('Webiators_GetDir); echo $modulePath; exit; die(); |
We hope that this post helps you. If you also want to get more information related to the Magneto 2 store, then you can also contact us, or you can take any information related to it using the comment section given below. Not only this, if you are worried about any other problem related to your Magento store, then it is not needed. We hope that our best magento ecommerce developers can help you in every way.
Thank You 😉