1
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

echo $storeManager->getStore()->getBaseUrl();exit;

This only gives the frontend base URL. How to get admin side URL?

6

You can use following code directly in your phtml file to get admin URL.

echo $this->helper('Magento\Backend\Helper\Data')->getHomePageUrl();
| improve this answer | |
  • 2
    Used method 1 , but i get the answer with multiple admin names....magento214new/admin/admin. – Rita Jose May 25 '17 at 4:59
  • Hello @DineshYadav your solution is fine to inject the helper inside constructor but we can also call the helper directly in PHTML file like echo $this->helper('Magento\Backend\Helper\Data')->getHomePageUrl(); is it not fine? – Sanchit Gupta Nov 8 '17 at 6:02
  • 1
    @SanchitGupta I have updated the answer. Thanks for your suggestion. :) – Dinesh Yadav Nov 8 '17 at 6:29
2
$this->helper('Magento\Backend\Helper\Data')->getAreaFrontName(); // admin_7asd32
$this->helper('Magento\Backend\Helper\Data')->getHomePageUrl(); // http://magento.com/admin/admin

Note that this helper is only available on templates.

| improve this answer | |
1

In the corresponding Block, create a function:

public function getAdminUrl()
{
    $route = "admin/dashboard/index/";
    $params = [];
    return $this->getUrl($route, $params);
}

In the corresponding phtml, echo it:

<?php echo $block->getAdminUrl(); ?>

Notice: The block should extends \Magento\Framework\View\Element\Template

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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