Accessing the admin front name programmatically in Magento 2

Published: March 23, 2017

Tags:

Recently I was working on some Magento 2 code where I needed to programmatically determine the admin front name. If you run a Google search you’ll pretty quickly find the canonical answer for Magento 1

Mage::getConfig()->getNode('admin/routers/adminhtml/args/frontName')

However, even after reading through two pages of Google results, I was not able to find an answer for Magento 2.

I decided to dig into the code myself and found that Magento\Framework\App\AreaList seems to be the best solution. Specifically the getFrontName method will give you the answer you need if you pass “adminhtml” as the $areaCode argument.

Here’s a quick example of how an implementation looks…

<?php

namespace Mpchadwick\Foo\Model;

use Magento\Framework\App\AreaList;

class Bar
{
    public $adminFrontName;
    
    public function __construct(AreaList $areaList)
    {
        $this->adminFrontName = $areaList->getFrontName('adminhtml')        
    }
}

Conclusion

If you have any questions or comments, feel free to drop a note below, or, as always, you can reach me on Twitter as well.

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.