2
class World extends \Magento\Framework\App\Action\Action
{    
    protected $logger;
    public function __construct(Context $context, \Psr\Log\LoggerInterface $logger)
    {        
         $this->logger = $logger;
        return parent::__construct($context);
    }
     public function execute()
    { 
          $this->logger->addDebug("TEST");
    }
}

In Magento 1 the controller action executes always when we go to the corresponding url. But in magento 2 "Test" gets logged only when i clear my cache and go to the controller url otherwise it doesn't. How can i add it to a controller in magento 2 so that it will keep on executing the code when i go to the corresponding url, without clearing the cache, like magento 1.

2
  • i faced same issue here.can u tell me what is the solution.i override catalog product view controller Jul 23, 2016 at 12:10
  • I'm really sorry i also couldn't figure out the exact issue. I created a new controller and it started working fine. Jul 23, 2016 at 17:17

1 Answer 1

0

Change your code by


$this->logger->debug("TEST");

5
  • Tried. Still it only prints if i clear my cache. Seems like the execute method gets executed if the cache is cleared. Jul 9, 2016 at 17:54
  • which version you tried? It's work with M2.10, M2.0.5
    – Sohel Rana
    Jul 9, 2016 at 18:29
  • I tried with Magento community edition 2.1. I think it's happening because of the magento full page cache. Jul 10, 2016 at 4:51
  • May be, But I tested enable/disable cache both.
    – Sohel Rana
    Jul 10, 2016 at 6:01
  • I created another controller and tried and it started working. The previous controller was returning $this->pageFactory->create(). Don't know why it didn't work. Jul 10, 2016 at 8:58

Your Answer

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

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