As a Magento 2 developer, one of the frequent tasks you’ll encounter is retrieving the Admin URL for your store. Whether you’re debugging, setting up a new environment, or automating deployments, knowing how to programmatically fetch the Admin URL can save you a lot of time.
In this tutorial, I’ll guide you through the various methods to get the Admin URL in Magento 2.
Why Do You Need the Admin URL?
The Admin URL is essential for accessing the backend of your Magento 2 store, where you manage products, orders, customers, and more. It’s also a key component when you’re working on integrations, custom modules, or automated scripts that require backend access.
Methods to Retrieve Admin URL in Magento 2
1. Using the Magento 2 Command Line (CLI)
The quickest way to get the Admin URL is via the Magento 2 CLI. This method is handy if you need to retrieve the Admin URL without accessing the Magento codebase.
1 |
php bin/magento info:adminuri |
2. Programmatically Retrieve the Admin URL
You can use the \Magento\Backend\Model\UrlInterface
class to retrieve the Admin URL in your custom module.
Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace Jigar\BackendDemoModule\Helper; use Magento\Backend\Model\UrlInterface; class Data { protected $backendUrl; public function __construct(UrlInterface $backendUrl) { $this->backendUrl = $backendUrl; } public function getAdminUrl() { return $this->backendUrl->getBaseUrl(); } } |
3. Fetching Admin URL via Database Query
Sometimes, you may need to retrieve the Admin URL directly from the database, especially when dealing with a large number of stores or when troubleshooting.
SQL Query:
1 |
SELECT value FROM core_config_data WHERE path = 'web/unsecure/base_url' AND scope = 'default'; |
That’s it. Hope you enjoyed the tutorial and the explanation.
You may also like,
How to Increase admin session timeout in magento 2?
How To Change The Admin Startup Page In Magento 2
Thank You.