Question:

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 (https://magento.stackexchange.com/questions/37089/disable-admin-or-frontend-route)

Answer:
Colton: 2 weeks ago

Module is tested

Bob, you can do this by magento event/observer (http://devdocs.magento.com/guides/v2.0/extension-dev-guide/events-and-observers.html).

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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="myObserverName" instance="[VendorName]\[ModuleName]\Observer\ObserverforDisabledFrontend" />
    </event>
</config>

Observer Code may be,

 <?php
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);
        /*$this->redirect->redirect($controller->getResponse(),
         'https://www.google.co.in/?gfe_rd=cr&ei=OHoGWLzeIqnT8geDpr3wDQ');*/

         $this->redirect->redirect($controller->getResponse(),$this->HelperBackend->getHomePageUrl());

    }

}

Note: Assume that your admin url is http://www.example.com/admin.

Full module is available at GitHub repo devamitbera/Magento2-extension (https://github.com/devamitbera/Magento2-extension/tree/master/Disabled-frontend)