Categories


Archives


Recent Posts


Categories


Magento 2: Base Directories

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Another consequence of losing the global Mage class in Magento 2 is the loss of the getBaseDir method. Fortunatly, the team behind Magento 2 has a solution, the MagentoAppDir object. You can use this object to get the base directory of your Magento system.

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

var_dump($base);

The obejct also comes with a set of constants for finding specific base dirs, and not just the Magento system’s base directory. For example, the following will find the base media folder

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

var_dump($base);

The Dir object also has a getUri method. This method finds a path suitable for use in an http string. Doing something like this

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

var_dump($base);

will return either the string media, or the string pub/media (depending on which index.php bootstrap file you’re using). If you try to use the getUri method with a constant that doesn’t make sense, the method returns false.

$object_manager = MagentoCoreModelObjectManager::getInstance();
$dir = $object_manager->get('MagentoAppDir');            
$base = $dir->getUri(MagentoAppDir::GENERATION);

var_dump($base);

For a full list of directory type constants see the lib/Magento/App/Dir.php source file

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

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 10th November 2013