How to Get Admin URL in Magento 2

How to Get Admin URL in Magento 2 - Jigar Karangiya

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.

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 :

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:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *