I write a plugin for check IP address and make a function aroundDispatch().

Let me clear more this is given below function.
This given one is cached.

public function aroundDispatch(
    \Magento\Framework\App\FrontControllerInterface $subject,
    \Closure $proceed,
    \Magento\Framework\App\RequestInterface $request
) {
    $scopeStores    = ScopeInterface::SCOPE_STORES;
    $currentStoreId = $this->storeManager->getStore()->getId();
    if (!$this->scopeConfig->isSetFlag('amgeoipredirect/general/enable', $scopeStores, $currentStoreId)
        || $this->isNeedToProceed($request)
    ) {
        return $proceed($request);


    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();

    $currentStoreId = $this->storeManager->getStore()->getId();

    if ($this->scopeConfig->getValue('amgeoipredirect/general/enable', $scopeStores, $currentStoreId)
        && $this->redirectAllowed
    ) {
        $location = $this->geolocation->locate($this->remoteAddress->getRemoteAddress());
        $country  = $location->getCountry();
        $session = $this->customerSession;
        $countRedirectStore    = 0;
        $countRedirectCurrency = 0;
        $countRedirectUrl      = 0;

Problem is IP is not changing, it is only changed when cache is fllush out.

How to stop caching this plugin file Anyone have any idea about this.



The best solution is use Ajax for get dynamically data.Another solution is Placeholder same as Hole punching in magento1. Read article

  • i want these two things $this->remoteAddress->getRemoteAddress() and country according to this ip dynamically. – Impunkj Sep 4 '17 at 10:09
  • ok,so make a controller and in this controller get all your data,ip,address whatever and call this controller via ajax and return result data. – akgola Sep 4 '17 at 10:25
  • How to get data in plugin file. i make a controller and set return value. But how to get in plugin file. – Impunkj Sep 4 '17 at 12:20
  • You can make a custom controller and call it from phtml file where you want the data and capture data dynamically from controller in phtml file. – akgola Sep 4 '17 at 12:29
  • I know but i want data in plugin file for aroundDispatch function. How can i do that – Impunkj Sep 5 '17 at 3:44

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.