Get Magento 2 Directory Paths

0
180
Get Magento 2 Directory Paths
Get Magento 2 Directory Paths

Again, in this post, I will show you how easy it is to get Magento 2 directory paths.

There are many directories in Magento 2 that you want to get in runtime like app, etc, var, pub … or even sub-directories like pub/static, pub/media/upload

To achieve this, you can inject an instance of Magento\Framework\App\Filesystem\DirectoryList then you can query all pre-defined Magento 2 paths.


protected $directoryList;
...

public function __construct(
    \Magento\Framework\Filesystem\DirectoryList $dirList,
) {
    ...
    $this->directoryList = $dirList;
    ...
}

public function someFunction()
{
    // Get `app` directory full path
    $this->directoryList->getPath('app'); 
    // Get `app/etc` path
    $this->directoryList->getPath('etc');
}

You can also use ObjectManager to get an instance of DirectoryList if you want, but not recommended.


$objectManager = Magento\Framework\App\ObjectManager::getInstance();
$directoryList = $objectManager->get('\Magento\Framework\App\Filesystem\DirectoryList');
// Get Magento 2 directory paths
$path = $directoryList->getPath('var');

Apparently, Magento 2 framework has setup a pre-defined list of key name for most directories. Below is the list:


self::ROOT => [parent::PATH => ''],
self::APP => [parent::PATH => 'app'],
self::CONFIG => [parent::PATH => 'app/etc'],
self::LIB_INTERNAL => [parent::PATH => 'lib/internal'],
self::VAR_DIR => [parent::PATH => 'var'],
self::CACHE => [parent::PATH => 'var/cache'],
self::LOG => [parent::PATH => 'var/log'],
self::DI => [parent::PATH => 'generated/metadata'],
self::GENERATION => [parent::PATH => Io::DEFAULT_DIRECTORY],
self::SESSION => [parent::PATH => 'var/session'],
self::MEDIA => [parent::PATH => 'pub/media', parent::URL_PATH => 'pub/media'],
self::STATIC_VIEW => [parent::PATH => 'pub/static', parent::URL_PATH => 'pub/static'],
self::PUB => [parent::PATH => 'pub', parent::URL_PATH => 'pub'],
self::LIB_WEB => [parent::PATH => 'lib/web'],
self::TMP => [parent::PATH => 'var/tmp'],
self::UPLOAD => [parent::PATH => 'pub/media/upload', parent::URL_PATH => 'pub/media/upload'],
self::TMP_MATERIALIZATION_DIR => [parent::PATH => 'var/view_preprocessed/pub/static'],
self::TEMPLATE_MINIFICATION_DIR => [parent::PATH => 'var/view_preprocessed'],
self::SETUP => [parent::PATH => 'setup/src'],
self::COMPOSER_HOME => [parent::PATH => 'var/composer_home'],
self::GENERATED => [parent::PATH => 'generated'],
self::GENERATED_CODE => [parent::PATH => Io::DEFAULT_DIRECTORY],
self::GENERATED_METADATA => [parent::PATH => 'generated/metadata'],

and these are the keys:


const ROOT = 'base';
const APP = 'app';
const CONFIG = 'etc';
const LIB_INTERNAL = 'lib_internal';
const LIB_WEB = 'lib_web';
const PUB = 'pub';
const MEDIA = 'media';
const STATIC_VIEW = 'static';
const VAR_DIR = 'var';
const TMP = 'tmp';
const CACHE = 'cache';
const LOG = 'log';
const SESSION = 'session';
const SETUP = 'setup';
const DI = 'di';
const GENERATION = 'generation';
const UPLOAD = 'upload';
const COMPOSER_HOME = 'composer_home';
const TMP_MATERIALIZATION_DIR = 'view_preprocessed';
const TEMPLATE_MINIFICATION_DIR = 'html';
const GENERATED = 'generated';
const GENERATED_CODE = 'code';
const GENERATED_METADATA = 'metadata';

You can refer to these directories at this class Magento\Framework\App\Filesystem\DirectoryList

Have fun!