I am using Magento version 2.3 Varnish 6 enabled Docker setup

I am implementing force login on event 'controller_action_predispatch'. Below is my code:


<?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="kernel_request_before" instance="Vendor\Module\Observer\FrontSendResponseBefore" shared="false" />



namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class FrontSendResponseBefore implements ObserverInterface
    private $_helper;

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

    private $_objectManager;

    public function __construct(
        \Vendor\Module\Helper\CoreHelper $helper,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\ObjectManagerInterface $objectmanager
    ) {
        $this->_helper = $helper;
        $this->redirect = $redirect;
        $this->_objectManager = $objectmanager;

    public function execute(\Magento\Framework\Event\Observer $observer)
        $actionName = $observer->getEvent()->getRequest()->getFullActionName();
        $controller = $observer->getControllerAction();

        $allowedUrls = array(

        $customerSession = $this->_objectManager->create('Magento\Customer\Model\SessionFactory')->create();

        $redirect = true;
        if (!$this->ifForceLoginEnabledForStore()) {
            $redirect = false;
        } else if (in_array($actionName, $allowedUrls)) {
            $redirect = false;
        } elseif ($customerSession->isLoggedIn()) {
            $redirect = false;

        echo 4; die;

        if ($redirect) {
            $this->redirect->redirect($controller->getResponse(), 'customer/account/login');

    private function ifForceLoginEnabledForStore()
        $storeId = $this->_helper->getCurrentStoreId();

        if ($this->_helper->getConfigValue('general/general/forcelogin', $storeId) == true) {

            return true;

        return false;

As you see I have added a die statement, but it does not execute when the cached version of page is loaded.

Does anyone has any idea how to fix this?

  • did you find any solution on this. I also have similar requirement Commented Feb 27, 2021 at 14:20

1 Answer 1


That predispatch event is not dispatched when loading cached content.

You could plugin to the load method, either before or after.

In di.xml:

<type name="Magento\Framework\App\PageCache\Cache">
    <plugin name="some-name" type="Vendor\Module\Model\App\PageCachePlugin"/>

In your new class:

public function afterLoad(
    \Magento\Framework\App\PageCache\Cache $subject,
) {
    ... //Do something here
    return $result;
  • Could you explain a bit more? What will the plugin of cache will do in my case?
    – anujeet
    Commented Jun 11, 2020 at 14:16
  • this way you can check your conditions and perform the redirect before or after loading the content from cache. As you noticed that the event you are targeting doesn't get dispatched when pulling the page content from the cache. Commented Jun 11, 2020 at 14:32
  • Will this work for Varnish as well?
    – anujeet
    Commented Jun 12, 2020 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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