Magento 2 Get Paths Programatically

Looking for Mage::getBaseUrl in Magento 2?
Use the DirectoryList class in your constructor to get an instance of DirectoryList.

Magento\Framework\App\Filesystem\DirectoryList $directory_list

For getting your Magento 2 root folder you can use


For getting media folder you can use



Other possible uses are:

/* Get app folder */
/* Get configuration folder */
/* Get libraries or third-party components folder */
/* Get libraries/components that need to be accessible publicly through web-server folder */
/* Get public folder */
/* Get static folder */
/* Get var folder */
/* Get temporary files folder */
/* Get file system caching directory (if file system caching is used) */
/* Get logs of system messages and errors */
/* Get file system session directory (if file system session storage is used) */
/* Get directory for Setup application*/
/* Get Dependency injection related file directory */
/* Relative directory key for generated code*/
/* Temporary directory for uploading files by end-user */
/* Directory to store composer related files (config, cache etc.) in case if composer runs by Magento Application */
/* A suffix for temporary materialization directory where pre-processed files will be written (if necessary) */
/* Get template minification dir */

Magento Indexers – Programmatically Using Indexers

In some custom modules, you may need to change the index mode from manual to auto or vice versa or you may need to run one of the indexers.

    = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($pCollection as $process) {

    = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($pCollection as $process) {

Indexes can be loaded using either the process ID or the process code using the functions:

by ID:

$process = Mage::getSingleton('index/indexer')->getProcessById();

by CODE:

$process = Mage::getSingleton('index/indexer')->getProcessByCode(<process_code>);


The process code and ID can be found by checking the table index_process. The Magento provided indexes normally have the following ID and code.

Process Name ID Code
Product Attributes 1 catalog_product_attribute
Product Prices 2 catalog_product_price
Catalog URL Rewrites 3 catalog_url
Product Flat Data 4 catalog_product_flat
Category Flat Data 5 catalog_category_flat
Category Products 6 catalog_category_product
Catalog Search Index 7 catalogsearch_stock
Stock Status 8 cataloginventory_stock
Tag Aggregation Data 9 tag_summary


mysqldump from CLI select tables

Suppose you need to take just a few tables from a database, like all the customer tables from a magento install. You just need to set up a few shell variables and you can mysqldump away! You’ll need your mysql database user name and password.

$:SQL="select group_concat(table_name separator ' ') from information_schema.tables where table_schema='$DBTODUMP' and table_name like 'customer%'"
$:TBLIST=`mysql -u db_user -pPASSWORD -AN -e"$SQL"`
$:mysqldump -u db_user -pPASSWORD $DBTODUMP $TBLIST > customer_tables.sql