Blog

Get Module Directory Path in Magento 2

To get the paths you need take help of \Magento\Framework\Module\Dir class as shown below,

<?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.

Leave a Reply

Your email address will not be published. Required fields are marked *

92 − 90 =

Popular Articles

Most Recent Posts

  • All Post
  • Blog
  • Business Supermarket
  • Creative
  • Design
  • Development
  • Digital
  • General
  • Marketing
  • Mobile Development
  • Online Marketing
  • SEO
  • Software Development Company
  • web design
  • Web Development
  • Website
  • websites design & development
  • WP React
  • WP Security