1

I want to use some data from intercepters in a block.

To start with I have attempted to use DI to inject a simple custom model but by logging I see that a instance is been used by the interceptor and the block.

What should I be doing to share data between the interceptor (plugin) and block given that this will always be in the same request lifecycle (i.e. not persisting between requests)?

2

You should be able to use the core registry for this purpose. Inject Magento\Framework\Registry into block and plug-in classes. Use it like this: $this->coreRegistry->register('my_data', $data); and $data = $this->coreRegistry->registry('my_data');

4
  • When I use $data = $this->coreRegistry->registry('my_data'); from the block class I don't see the expected value. Could the type of interceptor (around) be causing an issue? Oct 5 '16 at 8:00
  • Sounds like the call to access the data is happening before the data is set. It would help if you posted your code. Oct 5 '16 at 8:20
  • From logging I see the call to ->register('foo', 'bar') happens before ->registry('foo'). Could it be the coreRegistry is scoped? Oct 5 '16 at 15:20
  • Works very well for me - I have used it for sharing data between two plugins. Just the approach is not my favourite one, as it works like a global variable.
    – hey
    Apr 17 '19 at 3:19

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.