i can access session data in my controller using \Magento\Framework\Session\SessionManagerInterface
public function __construct(
Context $context,
PageFactory $resultPageFactory,
\Magento\Framework\Session\SessionManagerInterface $session
)
{
$this->resultPageFactory = $resultPageFactory;
$this->session = $session;
parent::__construct($context);
}
// assign session
then assess $this->session->setMessage("message") ;
In controller it is working . i need to retrieve session in view . how to i get that. $this->session->getMessage()
is not working in view
I need to check session values in phtml.
How to get session variable in magento.
Best Answer
Use a ViewModel to get session data into a view. https://www.yireo.com/blog/2017-08-12-viewmodels-in-magento-2
In your layout file:
In your viewModel file:
Then in your view
This is what is returned below: