I create route, and controller for it. All works fine if i call route like so.
....
return $this->_pageFactory->create();
....
However when I want get user data from Magento\Customer\Model\Session
inside controller i get error. I use $om to get data but i don`t know why it crash. I need user session for redirect to login form if user is not logged in.
...
$om = $context->getObjectManager();
$this->_customerSession = $om->get('Magento\Customer\Model\Session');
$this->_resultFactory = $context->getResultFactory();
...
in execute
if ($this->_customerSession->isLoggedIn()) {
return $this->_pageFactory->create();
}
$redirect = $this->_resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
$redirect->setUrl('/customer/account/create/');
return $redirect;
So question is how to redirect customer away from route if its not log in ?