Ikodes Technology

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.




Author Bio

ikodes Technology

Manager of Marketing for Red Stag Fulfillment, an ecommerce fulfillment warehouse that was born out of ecommerce. He has years of experience in ecommerce and business development. In his free time, Jake enjoys reading about business and sharing his own experience with others.

Go Back

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.

    Math Captcha 3 + 6 =

    Leave a Reply