How to get magento root file path in controller in Magento 2 ?

Like if my magento root file path is /var/www/html/magento in Controller file?

1 Answer 1


You can get like this one :

protected $_dir;

public function __construct(\Magento\Framework\Filesystem\DirectoryList $dir) 
    $this->_dir = $dir;

public yourfunction()
   $this->_dir->getRoot(); // Output: /var/www/html/magento
  • Thanks for your response it works @Dhiren
    – Prits
    Sep 2, 2020 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.