In this article, we are going to learn how we can get an admin path/URI programmatically.
Here, we can get the admin URI by using the following ways:
1. By using AreaList: In this method, we will create an instance of \Magento\Framework\App\AreaList class and call the getFrontName method as follows:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $areaList = $objectManager->get("\Magento\Framework\App\AreaList"); $adminPath = $areaList->getFrontName('adminhtml');
2. By using Backend Helper: In this method, we will create an instance of \Magento\Backend\Helper\Data class and call the getAdminFrontName method as follows:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $backendHelper = $objectManager->get("\Magento\Backend\Helper\Data"); $adminPath = $backendHelper->getAdminFrontName();
Note: The above-mentioned 1st method may not work in some cases, then you can go for the 2nd method. For example, if you have set wild card DNS entry settings for your instance and want to display a Magento admin login page link at frontend pages and frontend stores URLs may be like: https://maindomain.com or https://xyz.maindomain.com then, in this case, in the first case, for URL https://xyz.maindomain.com, you will not get any value for admin path URL, then you can use 2nd method(using Backend helper).
3. By using CLI: You can execute the following command in CLI to get the admin path URI:
php bin/magento info:adminuri
After executing the above command, the result will be as follows:
Magento 2 Company ? Read More
Admin URI: /admin_webkul
Hope this will be helpful.