Check session variable before delivering cached page

@odessite schrieb:

>I want to have the whole page visible only to people who have „logged in“.

Is it some custom controller or?

yes. The whole logic is in a class called FrontendSubscribere that i’ll attach below (at least the relavant parts)

class FrontendSubscriber implements SubscriberInterface
{
    public static function getSubscribedEvents() {
        return [
          'Enlight_Controller_Action_PreDispatch_Frontend' => 'onFrontendPreDispatch',
        ];
    }

    public function onFrontendPreDispatch(\Enlight_Event_EventArgs $arguments)
    {
        /** @var \Enlight_Controller_Action $controller */
        $controller = $arguments->get('subject');
        $controllerName = $controller->Request()->getControllerName();

        $passengerId = null;
        if(Shopware()->Session()) {
          $passengerId = Shopware()->Session()['passengerId'];
        }


        if(empty($passengerId) && $controllerName !== "passenger") {
            $controller->redirect('frontend/passenger/login');
        }
    }
}

So rather simple logic…

  • check if variable set in session
  • if it is not and the controller is not the custom controller that is responsible for „login“, do a redirect to the login page

all I need to do is execute this before the page is loaded from cache. (or maybe there is some other alternative that i’m not seeing at the moment)