How to disable the customer frontend in Magento 2?

Alexander: 2 weeks ago

My app only uses the Magento 2 REST API and the admin backend. How can I disable the frontend the customer interacts with?

PS: Seen this post, but it is an older one: Disable admin or frontend route (

Colton: 2 weeks ago

Module is tested

Bob, you can do this by magento event/observer (

For this, you need define an observer which will fire on event controller_action_predispatch .

Also note event should be define for frontend area,so that i will only working on frontend

So you need to define events.xml it at app/code/MyCompany/MyModule/etc/frontend

 <?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="myObserverName" instance="[VendorName]\[ModuleName]\Observer\ObserverforDisabledFrontend" />

Observer Code may be,

namespace [VendorName]\[ModuleName]\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverforDisabledFrontend implements ObserverInterface

     * @var \Magento\Framework\App\ActionFlag
    protected $_actionFlag;

     * @var \Magento\Framework\Message\ManagerInterface
    protected $messageManager;

     * @var \Magento\Framework\App\Response\RedirectInterface
    protected $redirect;

     * @var Magento\Backend\Helper\Data
    private $HelperBackend;

     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Backend\Helper\Data $HelperBackend
    ) {
        $this->_actionFlag = $actionFlag;
        $this->messageManager = $messageManager;
        $this->redirect = $redirect;
        $this->HelperBackend = $HelperBackend;

     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(\Magento\Framework\Event\Observer $observer)
        /** @var \Magento\Framework\App\Action\Action $controller */
        $controller = $observer->getControllerAction();
        $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);




Note: Assume that your admin url is

Full module is available at GitHub repo devamitbera/Magento2-extension (