How to get Magento version in Magento2 ? (Equivalent of Mage::getVersion())
You can use this in 2.0.x versions:
echo \Magento\Framework\AppInterface::VERSION;
For version 2.1:
Way 1, using DI:
public function __construct(
\Magento\Framework\App\ProductMetadataInterface $productMetadata
) {
$this->productMetadata = $productMetadata;
}
public function getMagentoVersion()
{
return $this->productMetadata->getVersion();
}
Way 2, using ObjectManager directly:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
echo $productMetadata->getVersion();
Up until Magento version 2.0.7 the version number was maintained in the AppInterface::VERSION
constant.
With the release of Magento 2.1 the constant was removed.
So till 2.0.7 if you check the adminhtml footer file where the version is shown
It had reference to the \Magento\Framework\AppInterface::VERSION
constant.
But since Magento 2.1 release the footer file now uses the \Magento\Backend\Block\Page\Footer::getMagentoVersion()
which in turn calls the \Magento\Framework\App\ProductMetadata::getVersion()
.
Previously the ProductMetadata::getVersion()
used to return the value of the constant \Magento\Framework\AppInterface::VERSION
, but now it parses the composer.json
as well as composer.lock
and returns the appropriate magento version
So no matter which version you are on either 2.0.x or 2.1.x, if you use the \Magento\Framework\App\ProductMetadata::getVersion()
method, you will always get the proper Magento version.
Conclusion:
Magento 1:
Mage::getVersion() //will return the magento version
Magento 2:
//Updated to use object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$version = $productMetadata->getVersion(); //will return the magento version
On top of the other answers, you can get the major version (for example 2.1) by accessing /magento_version
on your Magento 2 website.