Retrieve A Module Filesystem Path Programmatically

In this post we would have a look again on you can do things programatically in Magento 2. Let's see then on how you can retrieve a module filesystem path programmatically!

Responsible for that is the class Magento\Framework\Module\Dir\Reader in which the function getModuleDir($type, $moduleName) will retrieve you the full path to a directory of certain type within a module. As you see it takes 2 parameters, the type which is the module's folder you want to retrieve and the $moduleName which is the module's name. The $type parameter can be empty or have one of the following values

  • 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';

and are declared in the class Magento\Framework\Module\Dir.

So for example to retrieve the path of the etc folder of the module MageVision_PredefinedAdminOrderComments, you will need to call

getModuleDir(Magento\Framework\Module\Dir::MODULE_ETC_DIR, 'MageVision_PredefinedAdminOrderComments');

Feel free to share this post and ask your questions in the comments below.

Till next time!