28
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath(); 

it returns media path. How to get the root path of a project in a phtml page?

0

4 Answers 4

68

Class \Magento\Framework\Filesystem\DirectoryList is used to get path like root, media, var etc

This will get root directory of your project like this

/var/www/html/myproject

By ObjectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList'); echo $rootPath = $directory->getRoot(); 

By Dependency Injection

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

Get other directory paths like

$this->_dir->getRoot(); // Output: /var/www/html/myproject $this->_dir->getPath('media'); // Output: /var/www/html/myproject/pub/media $this->_dir->getPath('pub'); // Output: /var/www/html/myproject/pub $this->_dir->getPath('static'); // Output: /var/www/html/myproject/pub/static $this->_dir->getPath('var'); // Output: /var/www/html/myproject/var $this->_dir->getPath('app'); // Output: /var/www/html/myproject/app $this->_dir->getPath('etc'); // Output: /var/www/html/myproject/app/etc $this->_dir->getPath('lib_internal'); // Output: /var/www/html/myproject/lib/internal $this->_dir->getPath('lib_web'); // Output: /var/www/html/myproject/lib/web $this->_dir->getPath('tmp'); // Output: /var/www/html/myproject/var/tmp $this->_dir->getPath('cache'); // Output: /var/www/html/myproject/var/cache $this->_dir->getPath('log'); // Output: /var/www/html/myproject/var/log $this->_dir->getPath('session'); // Output: /var/www/html/myproject/var/session $this->_dir->getPath('setup'); // Output: /var/www/html/myproject/setup/src $this->_dir->getPath('di'); // Output: /var/www/html/myproject/var/di $this->_dir->getPath('upload'); // Output: /var/www/html/myproject/pub/media/upload $this->_dir->getPath('generation'); // Output: /var/www/html/myproject/var/generation $this->_dir->getPath('view_preprocessed'); // Output: /var/www/html/myproject/var/view_preprocessed $this->_dir->getPath('composer_home'); // Output: /var/www/html/myproject/var/composer_home $this->_dir->getPath('html'); // Output: /var/www/html/myproject/var/view_preprocessed/html 

NOTE: You should never use \Magento\Framework\App\ObjectManager::getInstance() It defeats the purpose of dependency injection.

1
  • factory method not working ... Commented Jun 20, 2018 at 11:12
30

You can use magento 2 default variable to get absolute path of Magento directory

echo BP; 
1
  • 2
    Most clever and practical answer Commented Apr 10, 2020 at 16:10
2

You have to use ROOT for get root directory

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $fileSystem = $objectManager->get('Magento\Framework\Filesystem'); $mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::ROOT)->getAbsolutePat‌​h() 
2
  • please let me know if you have issue Commented Jun 1, 2017 at 5:55
  • to get the result need to add ->getAbsolutePath(); to the last code $mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::ROOT)->getAbsolutePath(); Commented Jun 1, 2017 at 6:26
2

I prefer Alan Storm's approach.

$object_manager = MagentoCoreModelObjectManager::getInstance(); $dir = $object_manager->get('MagentoAppDir'); $base = $dir->getDir(); $media = $dir->getUrl(MagentoAppDir::MEDIA); 

And you can find a full list of constants under lib/Magento/App/Dir.php.

#File: lib/Magento/App/Dir.php /** * Code base root */ const ROOT = 'base'; /** * Most of entire application */ const APP = 'app'; /** * Modules */ const MODULES = 'code'; /** * Themes */ const THEMES = 'design'; /** * Initial configuration of the application */ const CONFIG = 'etc'; /** * Libraries or third-party components */ const LIB = 'lib'; /** * Files with translation of system labels and messages from en_US to other languages */ const LOCALE = 'i18n'; /** * Directory within document root of a web-server to access static view files publicly */ const PUB = 'pub'; /** * Libraries/components that need to be accessible publicly through web-server (such as various DHTML components) */ const PUB_LIB = 'pub_lib'; /** * Storage of files entered or generated by the end-user */ const MEDIA = 'media'; /** * Storage of static view files that are needed on HTML-pages, emails or similar content */ const STATIC_VIEW = 'static'; /** * Public view files, stored to avoid repetitive run-time calculation, and can be re-generated any time */ const PUB_VIEW_CACHE = 'view_cache'; /** * Various files generated by the system in runtime */ const VAR_DIR = 'var'; /** * Temporary files */ const TMP = 'tmp'; /** * File system caching directory (if file system caching is used) */ const CACHE = 'cache'; /** * Logs of system messages and errors */ const LOG = 'log'; /** * File system session directory (if file system session storage is used) */ const SESSION = 'session'; /** * Dependency injection related file directory * */ const DI = 'di'; /** * Relative directory key for generated code */ const GENERATION = 'generation'; /** * Temporary directory for uploading files by end-user */ const UPLOAD = 'upload'; 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.