2

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

  • bob, code is updated,please check – Amit Bera Oct 18 '16 at 20:12
2

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="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

| improve this answer | |
  • Thanks mate, I'm going to dig into this, will get back to it asap. – Bob van Luijt Oct 18 '16 at 13:22
  • 1
    Hi @Amit unfortunately this disables everything, the REST api and admin aren't available now. – Bob van Luijt Oct 20 '16 at 19:30
  • 1
    bob, i have check in default magento 2.1, it is working – Amit Bera Oct 23 '16 at 11:01
  • @BobvanLuijt Did you get the solution? – Muhsin May 11 '17 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.