Magento 2 – base directories
This tutorial is about Magento 2 base directories. In my last post about Magento 1 base directories I showed you a full list of possible directories. I suggested you to always use this method to get the correct path. In Magento 2 the same suggestion is true. The only difference is the method to get the needed path. I show you now how this works in new Magento version.
Magento 2 – base directories
The source for this is vendor/magento/framework/App/Filesystem/DirectoryList.php. It contains constants for each needed Magento folder. It is very easy to use DirectoryList object in you own code. The simplest option is to get it from you objectManager:
1 2 3 4 | $objectManager = Magento\Framework\App\ObjectManager::getInstance(); $directoryList = $objectManager->get('\Magento\Framework\App\Filesystem\DirectoryList'); $path = $directoryList->getPath('var'); var_dump($path); |
The same rules as for Magento 1 are true. All paths are without a trailing slash at the end! So if you want to get a file path or sub-folder, remember to add it.
Suggested method
Getting a directory list object by object manager works but is not a good idea if you implement your own extension and use it on many places. For this it is recommended to inject this model into you model. This works as follows:
1 2 3 4 5 6 7 8 9 10 11 | class MyClass { protected $_directoryList; public function __construct( \Magento\Framework\App\Filesystem\DirectoryList $directoryList, ... ){ $this->_directoryList = $directoryList; ... } |
You can later use it with the following line of code:
1 | $path = $this->_directoryList->getPath('var'); |
Possible paths
You can get all relevant Magento 2 base directories, but they are completely different to Magento 1. The source shows the following complete list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $result = [ 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 => 'var/di'], self::GENERATION => [parent::PATH => 'var/generation'], 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'], self::TEMPLATE_MINIFICATION_DIR => [parent::PATH => 'var/view_preprocessed/html'], self::SETUP => [parent::PATH => 'setup/src'], self::COMPOSER_HOME => [parent::PATH => 'var/composer_home'], ]; |
No getBaseDir anymore?
The best known getBaseDir method is not available anymore. The complete Mage class from Magento 1 was removed and replaced by a completely new system of objects. Magento 2 is completely rewritten, so you may find only little know. Don’t be afraid to learn new methods, Magento 2 has many advantages and is now future ready.
Conclusion
Magento 2 base directories are different to old shop system versions. I showed you how to get it by code.