Sometimes we need to get module’s directory path mainly when we working with libraries. In this article, I will show how we can get the module’s directory path. Check this article for Get root directory path in Magento 2
Use Magento\Framework\Module\Dir
class to get the module’s directory paths.
namespace MagePrince\Testing\Model; class Directories { private $directory; public function __construct( \Magento\Framework\Module\Dir $directory ) { $this->directory = $directory; } public function getModuleEtcDir() { $directoryPath = $this->directory->getDir( 'MagePrince_Testing', Dir::MODULE_ETC_DIR ); return $directoryPath; } }
By the above getModuleEtcDir()
function you can get the module’s etc folder path like:
/var/www/html/magento2/app/code/MagePrince/Testing/etc
You can use the following constant of Magento\Framework\Module\Dir
class to get module’s directory paths
const MODULE_ETC_DIR = 'etc';
const MODULE_I18N_DIR = 'i18n';
const MODULE_VIEW_DIR = 'view';
const MODULE_CONTROLLER_DIR = 'Controller';
const MODULE_SETUP_DIR = 'Setup';
Hope this article will help you to understand how to get Module’s directory paths. If you have any question please comment below.
Happy Coding…:)