
Get Module Directory Path in Magento 2
To get the paths you need take help of \Magento\Framework\Module\Dir class as shown below,
If we do not provide any second parameter then getDir method will return path to module’s directory. We can provide the second parameter to get the specific folders of a module.
Thanks for reading. Feel free to contact usContact if you face any issue.
<?php
namespace ikodes\ModuleName\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Module\Dir;
class Blog extends Action
{
public function __construct(
Context $context,
Dir $moduleDir
) {
$this->moduleDir = $moduleDir;
parent::__construct($context);
}
public function execute()
{
$modulePath = $this->moduleDir->getDir('Ikodes_ModuleName');
$moduleEtcPath = $this->moduleDir->getDir('Ikodes_ModuleName', Dir::MODULE_ETC_DIR);
$moduleI18nPath = $this->moduleDir->getDir('Ikodes_ModuleName', Dir::MODULE_I18N_DIR);
$moduleViewPath = $this->moduleDir->getDir('Ikodes_ModuleName', Dir::MODULE_VIEW_DIR);
$moduleControllerPath = $this->moduleDir->getDir('Ikodes_ModuleName', Dir::MODULE_CONTROLLER_DIR);
$moduleSetupPath = $this->moduleDir->getDir('Ikodes_ModuleName', Dir::MODULE_SETUP_DIR);
}
}
If we do not provide any second parameter then getDir method will return path to module’s directory. We can provide the second parameter to get the specific folders of a module.
Thanks for reading. Feel free to contact usContact if you face any issue.
Expand Your Digital Horizons With Us
Start a new project or take an existing one to the next level. Get in touch to start
small, scale-up, and go Agile.